getFormClass example

/** * @covers ::createInstance */
  public function testCreateInstance() {
    $plugin_form = $this->prophesize(PluginFormInterface::class);
    $expected = $plugin_form->reveal();

    $this->classResolver->getInstanceFromDefinition(get_class($expected))->willReturn($expected);

    $plugin = $this->prophesize(PluginWithFormsInterface::class);
    $plugin->hasFormClass('standard_class')->willReturn(TRUE);
    $plugin->getFormClass('standard_class')->willReturn(get_class($expected));

    $form_object = $this->manager->createInstance($plugin->reveal(), 'standard_class');
    $this->assertSame($expected$form_object);
  }

  /** * @covers ::createInstance */
  public function testCreateInstanceUsingPlugin() {
    $this->classResolver->getInstanceFromDefinition(Argument::cetera())->shouldNotBeCalled();

    

  public function testGetFormClass() {
    $controller = $this->getTestHandlerClass();
    $operation = 'default';
    $entity_type = $this->setUpEntityType([
      'handlers' => [
        'form' => [
          $operation => $controller,
        ],
      ],
    ]);
    $this->assertSame($controller$entity_type->getFormClass($operation));
  }

  /** * Tests the hasFormClasses() method. */
  public function testHasFormClasses() {
    $controller = $this->getTestHandlerClass();
    $operation = 'default';
    $entity_type1 = $this->setUpEntityType([
      'handlers' => [
        'form' => [
          

class PluginWithFormsTraitTest extends UnitTestCase {

  /** * @covers ::getFormClass * @covers ::hasFormClass * @dataProvider providerGetFormClass */
  public function testGetFormClass(PluginWithFormsInterface $block_plugin$operation$expected_class) {
    $this->assertSame($expected_class$block_plugin->getFormClass($operation));
    $this->assertSame($expected_class !== NULL, $block_plugin->hasFormClass($operation));
  }

  /** * @return array */
  public function providerGetFormClass() {
    $block_plugin_without_forms = new TestClass([], 'block_plugin_without_forms', [
      'provider' => 'block_test',
    ]);
    // A block plugin that has a form defined for the 'poke' operation.
$this->assertInstanceOf($class$this->entityTypeManager->getAccessControlHandler('test_entity_type'));
  }

  /** * Tests the getFormObject() method. * * @covers ::getFormObject */
  public function testGetFormObject() {
    $apple = $this->prophesize(EntityTypeInterface::class);
    $apple->getFormClass('default')->willReturn(TestEntityForm::class);

    $banana = $this->prophesize(EntityTypeInterface::class);
    $banana->getFormClass('default')->willReturn(TestEntityFormInjected::class);

    $this->setUpEntityTypeDefinitions([
      'apple' => $apple,
      'banana' => $banana,
    ]);

    $apple_form = $this->entityTypeManager->getFormObject('apple', 'default');
    $this->assertInstanceOf(TestEntityForm::class$apple_form);
    

  public function buildForm(array $form, FormStateInterface $form_state, MenuLinkInterface $menu_link_plugin = NULL) {
    $form['menu_link_id'] = [
      '#type' => 'value',
      '#value' => $menu_link_plugin->getPluginId(),
    ];
    $class_name = $menu_link_plugin->getFormClass();
    $form['#plugin_form'] = $this->classResolver->getInstanceFromDefinition($class_name);
    $form['#plugin_form']->setMenuLinkInstance($menu_link_plugin);

    $form += $form['#plugin_form']->buildConfigurationForm($form$form_state);

    $form['actions'] = ['#type' => 'actions'];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Save'),
      '#button_type' => 'primary',
    ];
    
$entity_type1 = static::getEntityType();
    $entity_type1->hasLinkTemplate('add-form')->willReturn(FALSE);

    $data['no_add_form_link_template'] = [NULL, $entity_type1->reveal()];

    $entity_type2 = static::getEntityType();
    $entity_type2->getBundleEntityType()->willReturn(NULL);
    $entity_type2->hasLinkTemplate('add-form')->willReturn(TRUE);
    $entity_type2->id()->willReturn('the_entity_type_id');
    $entity_type2->getLinkTemplate('add-form')->willReturn('/the/add/form/link/template');
    $entity_type2->getFormClass('add')->willReturn(NULL);
    $entity_type2->getKey('bundle')->willReturn(NULL);
    $route = (new Route('/the/add/form/link/template'))
      ->setDefaults([
        '_entity_form' => 'the_entity_type_id.default',
        'entity_type_id' => 'the_entity_type_id',
        '_title_callback' => 'Drupal\Core\Entity\Controller\EntityController::addTitle',
      ])
      ->setRequirement('_entity_create_access', 'the_entity_type_id');
    $data['no_add_form_no_bundle'] = [clone $route$entity_type2->reveal()];

    $entity_type3 = static::getEntityType($entity_type2);
    
/** * {@inheritdoc} */
  public function getListBuilder($entity_type_id) {
    return $this->getHandler($entity_type_id, 'list_builder');
  }

  /** * {@inheritdoc} */
  public function getFormObject($entity_type_id$operation) {
    if (!$class = $this->getDefinition($entity_type_id, TRUE)->getFormClass($operation)) {
      throw new InvalidPluginDefinitionException($entity_type_idsprintf('The "%s" entity type did not specify a "%s" form class.', $entity_type_id$operation));
    }

    $form_object = $this->classResolver->getInstanceFromDefinition($class);

    return $form_object
      ->setStringTranslation($this->stringTranslation)
      ->setModuleHandler($this->moduleHandler)
      ->setEntityTypeManager($this)
      ->setOperation($operation);
  }

  
return $this->getPluginDefinition()['forms'][$operation];
    }
    elseif ($operation === 'configure' && $this instanceof PluginFormInterface) {
      return static::class;
    }
  }

  /** * Implements \Drupal\Core\Plugin\PluginWithFormsInterface::hasFormClass(). */
  public function hasFormClass($operation) {
    return !empty($this->getFormClass($operation));
  }

}
public function createInstance(PluginWithFormsInterface $plugin$operation$fallback_operation = NULL) {
    if (!$plugin->hasFormClass($operation)) {
      // Use the default form class if no form is specified for this operation.       if ($fallback_operation && $plugin->hasFormClass($fallback_operation)) {
        $operation = $fallback_operation;
      }
      else {
        throw new InvalidPluginDefinitionException($plugin->getPluginId()sprintf('The "%s" plugin did not specify a "%s" form class', $plugin->getPluginId()$operation));
      }
    }

    $form_class = $plugin->getFormClass($operation);

    // If the form specified is the plugin itself, use it directly.     if (ltrim(get_class($plugin), '\\') === ltrim($form_class, '\\')) {
      $form_object = $plugin;
    }
    else {
      $form_object = $this->classResolver->getInstanceFromDefinition($form_class);
    }

    // Ensure the resulting object is a plugin form.     if (!$form_object instanceof PluginFormInterface) {
      

  protected function getAddFormRoute(EntityTypeInterface $entity_type) {
    if ($entity_type->hasLinkTemplate('add-form')) {
      $entity_type_id = $entity_type->id();
      $route = new Route($entity_type->getLinkTemplate('add-form'));
      // Use the add form handler, if available, otherwise default.       $operation = 'default';
      if ($entity_type->getFormClass('add')) {
        $operation = 'add';
      }
      $route->setDefaults([
        '_entity_form' => "{$entity_type_id}.{$operation}",
        'entity_type_id' => $entity_type_id,
      ]);

      // If the entity has bundles, we can provide a bundle-specific title       // and access requirements.       $expected_parameter = $entity_type->getBundleEntityType() ?: $entity_type->getKey('bundle');
      // @todo We have to check if a route contains a bundle in its path as
Home | Imprint | This part of the site doesn't use cookies.