getFormDisplay example



  /** * Tests widget constraint validation. */
  public function testValidation() {
    $entity_type = 'entity_test_constraint_violation';
    $entity = $this->container->get('entity_type.manager')
      ->getStorage($entity_type)
      ->create(['id' => 1, 'revision_id' => 1]);
    $display = \Drupal::service('entity_display.repository')
      ->getFormDisplay($entity_type$entity_type);
    $form = [];
    $form_state = new FormState();
    $display->buildForm($entity$form$form_state);

    // Pretend the form has been built.     $form_state->setFormObject(\Drupal::entityTypeManager()->getFormObject($entity_type, 'default'));
    \Drupal::formBuilder()->prepareForm('field_test_entity_form', $form$form_state);
    \Drupal::formBuilder()->processForm('field_test_entity_form', $form$form_state);

    // Validate the field constraint.     $form_state->getFormObject()->setEntity($entity)->setFormDisplay($display$form_state);
    
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ])->save();

    $handler_settings = [
      'target_bundles' => [
        $vocabulary->id() => $vocabulary->id(),
      ],
    ];
    $this->createEntityReferenceField('node', 'article', 'field_' . $vocabulary->id(), 'Tags', 'taxonomy_term', 'default', $handler_settings);

    \Drupal::service('entity_display.repository')
      ->getFormDisplay('node', 'article')
      ->setComponent('field_' . $vocabulary->id())
      ->save();

    // Setup node access testing.     node_access_rebuild();
    node_access_test_add_field(NodeType::load('article'));
    \Drupal::state()->set('node_access_test.private', TRUE);

  }

  /** * Runs the field CRUD tests. * * In order to act on the same fields, and not create the fields over and over * again the following tests create, update and delete the same fields. */
$plugin_type_info = $plugin->getPluginDefinition();
      $field_row['plugin']['settings_edit_form']['label']['#markup'] = $this->t('Widget settings:') . ' <span class="plugin-name">' . $plugin_type_info['label'] . '</span>';
    }

    return $field_row;
  }

  /** * {@inheritdoc} */
  protected function getEntityDisplay($entity_type_id$bundle$mode) {
    return $this->entityDisplayRepository->getFormDisplay($entity_type_id$bundle$mode);
  }

  /** * {@inheritdoc} */
  protected function getDefaultPlugin($field_type) {
    return $this->fieldTypes[$field_type]['default_widget'] ?? NULL;
  }

  /** * {@inheritdoc} */
$field = FieldConfig::create([
      'field_storage' => $field_storage,
      'label' => 'Some user field',
      'bundle' => 'user',
      'required' => TRUE,
    ]);
    $field->save();

    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');

    $display_repository->getFormDisplay('user', 'user')
      ->setComponent('test_user_field', ['type' => 'test_field_widget'])
      ->save();
    $display_repository->getFormDisplay('user', 'user', 'register')
      ->save();

    // Check that the field does not appear on the registration form.     $this->drupalGet('user/register');
    $this->assertSession()->pageTextNotContains($field->label());
    $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', 'config:core.entity_form_display.user.user.register');
    $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', 'config:user.settings');

    
'type' => 'test_field',
      'entity_type' => $this->entityTypeId,
      'cardinality' => 1,
    ]);
    $field_storage->save();
    FieldConfig::create([
      'field_storage' => $field_storage,
      'bundle' => $this->bundle,
      'label' => 'Untranslatable-but-visible test field',
      'translatable' => FALSE,
    ])->save();
    \Drupal::service('entity_display.repository')->getFormDisplay($this->entityTypeId, $this->bundle, 'default')
      ->setComponent('field_multilingual', [
        'type' => 'test_field_widget_multilingual',
      ])
      ->save();
  }

  /** * Tests that hiding untranslatable field widgets works correctly. */
  public function testHiddenWidgets() {
    /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
    
$id = 'test_different_displays';
    $field_name = 'field_media_different_display';

    $this->createMediaTypeViaForm($id$field_name);

    // Source field not in displays.     $display = \Drupal::service('entity_display.repository')->getViewDisplay('media', $id);
    $components = $display->getComponents();
    $this->assertArrayHasKey($field_name$components);
    $this->assertSame('entity_reference_entity_id', $components[$field_name]['type']);

    $display = \Drupal::service('entity_display.repository')->getFormDisplay('media', $id);
    $components = $display->getComponents();
    $this->assertArrayHasKey($field_name$components);
    $this->assertSame('entity_reference_autocomplete_tags', $components[$field_name]['type']);
  }

  /** * Tests hidden source field in media type. */
  public function testHiddenSourceField() {
    $id = 'test_hidden_source_field';
    $field_name = 'field_media_hidden';

    
$handler_settings = [
      'target_bundles' => [
        $vocabulary->id() => $vocabulary->id(),
      ],
      'auto_create' => TRUE,
    ];
    $this->createEntityReferenceField('node', 'page', $this->fieldName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);

    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');

    $display_repository->getFormDisplay('node', 'page')
      ->setComponent($this->fieldName, [
        'type' => 'entity_reference_autocomplete_tags',
      ])
      ->save();

    // Show on default display and teaser.     $display_repository->getViewDisplay('node', 'page')
      ->setComponent($this->fieldName, [
        'type' => 'entity_reference_label',
      ])
      ->save();
    
$field_name = $this->fieldStorage->getName();
    $field_label = $this->field->label();

    // Ensure field is set to a date only field.     $this->fieldStorage->setSetting('datetime_type', DateRangeItem::DATETIME_TYPE_DATE);
    $this->fieldStorage->save();

    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');

    // Change the widget to a datelist widget.     $display_repository->getFormDisplay($this->field->getTargetEntityTypeId()$this->field->getTargetBundle())
      ->setComponent($field_name[
        'type' => 'daterange_datelist',
        'settings' => [
          'date_order' => 'YMD',
        ],
      ])
      ->save();
    \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();

    // Display creation form.     $this->drupalGet('entity_test/add');
    
'field_type' => 'entity_reference',
      'required' => TRUE,
      'settings' => [
        'handler_settings' => [
          'target_bundles' => [
            'type_three' => 'type_three',
          ],
        ],
      ],
    ])->save();

    $display_repository->getFormDisplay('node', 'basic_page')
      ->setComponent('media_image_field', [
        'type' => 'media_library_widget',
        'region' => 'content',
        'settings' => [
          'media_types' => ['type_three'],
        ],
      ])
      ->save();

    $this->config('media_library.settings')
      ->set('advanced_ui', TRUE)
      
$this->createEntityReferenceField(
      'node',
      'article',
      'entity_reference_comment',
      'Entity Reference Comment',
      'comment',
      'default',
      ['target_bundles' => ['comment']]
    );
    \Drupal::service('entity_display.repository')
      ->getFormDisplay('node', 'article')
      ->setComponent('entity_reference_comment', ['type' => 'options_select'])
      ->save();
    \Drupal::service('entity_display.repository')
      ->getViewDisplay('node', 'article')
      ->setComponent('entity_reference_comment', ['type' => 'entity_reference_label'])
      ->save();

    $administratorUser = $this->drupalCreateUser([
      'skip comment approval',
      'post comments',
      'access comments',
      
$handler_settings = [
      'target_bundles' => [
        $this->vocabulary->id() => $this->vocabulary->id(),
      ],
      'auto_create' => TRUE,
    ];
    $this->createEntityReferenceField('node', 'article', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
    $this->field = FieldConfig::loadByName('node', 'article', $field_name);

    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');
    $display_repository->getFormDisplay('node', 'article')
      ->setComponent($field_name[
        'type' => 'options_select',
      ])
      ->save();
    $display_repository->getViewDisplay('node', 'article')
      ->setComponent($field_name[
        'type' => 'entity_reference_label',
      ])
      ->save();
  }

  
'bundle' => 'page',
      'label' => $this->randomMachineName() . '_label',
      'settings' => [
        'display_summary' => TRUE,
        'required_summary' => TRUE,
      ],
    ])->save();

    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');

    $display_repository->getFormDisplay('node', 'page')
      ->setComponent($field_name[
        'type' => 'text_textarea_with_summary',
      ])
      ->save();

    $this->drupalGet('node/add/page');
    $page = $this->getSession()->getPage();
    $summary_field = $page->findField('edit-' . $field_name . '-0-summary');

    $this->assertEquals(TRUE, $summary_field->isVisible());
  }

}
'entity_type' => 'node',
      'type' => 'text',
    ]);
    $this->fieldStorage->save();
    FieldConfig::create([
      'field_storage' => $this->fieldStorage,
      'bundle' => $this->contentType->id(),
    ])->save();
    $display_repository = $this->container->get('entity_display.repository');
    // Update the displays so that configuration does not change unexpectedly on     // import.     $display_repository->getFormDisplay('node', $this->contentType->id(), 'default')
      ->setComponent($this->fieldName, [
        'type' => 'text_textfield',
      ])
      ->save();
    $display_repository->getViewDisplay('node', $this->contentType->id(), 'full')
      ->setComponent($this->fieldName)
      ->save();
    $display_repository->getViewDisplay('node', $this->contentType->id(), 'default')
      ->setComponent($this->fieldName)
      ->save();
    $display_repository->getViewDisplay('node', $this->contentType->id(), 'teaser')
      
'type' => 'file',
      'cardinality' => 1,
    ])->save();
    FieldConfig::create([
      'field_name' => $field_file_name,
      'entity_type' => 'node',
      'bundle' => 'page',
      'label' => 'Test file',
      'required' => TRUE,
    ])->save();

    \Drupal::service('entity_display.repository')->getFormDisplay('node', 'page', 'default')
      ->setComponent($field_name['type' => 'text_textfield'])
      ->setComponent($field_file_name['type' => 'file_generic'])
      ->save();

    // Log in a user who can create 'page' nodes.     $this->webUser = $this->drupalCreateUser(['create page content']);
    $this->drupalLogin($this->webUser);

    // Get the form for adding a 'page' node. Submit an "add another item" Ajax     // submission and verify it worked by ensuring the updated page has two text     // field items in the field for which we just added an item.
FieldConfig::create([
      'field_name' => $this->referenceFieldName,
      'bundle' => $this->referrerType->id(),
      'entity_type' => $this->testEntityTypeName,
    ])
      ->save();

    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');

    $display_repository->getFormDisplay($this->testEntityTypeName, $this->referrerType->id())
      ->setComponent($this->referenceFieldName, [
        'type' => 'entity_reference_autocomplete',
      ])
      ->save();
    $display_repository->getViewDisplay($this->testEntityTypeName, $this->referrerType->id())
      ->setComponent($this->referenceFieldName, [
        'type' => 'entity_reference_label',
      ])
      ->save();
  }

  
Home | Imprint | This part of the site doesn't use cookies.