getComponent example

    // of selecting a replacement style by setting the replacement image style     // ID in the image style storage.     /** @var \Drupal\image\ImageStyleStorageInterface $storage */
    $storage = $this->container->get('entity_type.manager')->getStorage($style->getEntityTypeId());
    $storage->setReplacementId('main_style', 'replacement_style');
    $style->delete();

    // Check that the entity displays exists after dependency removal.     $this->assertNotNull($view_display = EntityViewDisplay::load($view_display->id()));
    $this->assertNotNull($form_display = EntityFormDisplay::load($form_display->id()));
    // Check that the 'sticker' formatter component exists in both displays.     $this->assertNotNull($formatter = $view_display->getComponent('sticker'));
    $this->assertNotNull($widget = $form_display->getComponent('sticker'));
    // Check that both displays are using now 'replacement_style' for images.     $this->assertSame('replacement_style', $formatter['settings']['image_style']);
    $this->assertSame('replacement_style', $widget['settings']['preview_image_style']);

    // Delete the 'replacement_style' without setting a replacement image style.     $replacement->delete();

    // The entity view and form displays exists after dependency removal.     $this->assertNotNull($view_display = EntityViewDisplay::load($view_display->id()));
    $this->assertNotNull($form_display = EntityFormDisplay::load($form_display->id()));
    
$data[] = ['date_format' => 'custom', 'custom_date_format' => 'e', 'timezone' => 'Asia/Tokyo'];

    foreach ($data as $settings) {
      [$date_format$custom_date_format$timezone] = array_values($settings);
      if (empty($timezone)) {
        $timezone = NULL;
      }

      $value = REQUEST_TIME - 87654321;
      $expected = \Drupal::service('date.formatter')->format($value$date_format$custom_date_format$timezone);

      $component = $this->display->getComponent($this->fieldName);
      $component['type'] = 'timestamp';
      $component['settings'] = $settings;
      $this->display->setComponent($this->fieldName, $component);

      $entity = EntityTest::create([]);
      $entity->{$this->fieldName}->value = $value;

      $this->renderEntityFields($entity$this->display);
      $this->assertRaw($expected);
    }
  }

  
if ($thumbnail_uri = $source->getMetadata($media$plugin_definition['thumbnail_uri_metadata_attribute'])) {
      $element['preview']['thumbnail'] = [
        '#theme' => 'image_style',
        '#style_name' => 'media_library',
        '#uri' => $thumbnail_uri,
      ];
    }

    $form_display = EntityFormDisplay::collectRenderDisplay($media, 'media_library');
    // When the name is not added to the form as an editable field, output     // the name as a fixed element to confirm the right file was uploaded.     if (!$form_display->getComponent('name')) {
      $element['fields']['name'] = [
        '#type' => 'item',
        '#title' => $this->t('Name'),
        '#markup' => $media->getName(),
      ];
    }
    $form_display->buildForm($media$element['fields']$form_state);

    // Add source field name so that it can be identified in form alter and     // widget alter hooks.     $element['fields']['#source_field_name'] = $this->getSourceFieldName($media->bundle->entity);

    
/** * Asserts various aspects of a comment component in an entity form display. * * @param string $id * The entity ID. * @param string $component_id * The ID of the form component. * * @internal */
  protected function assertDisplay(string $id, string $component_id): void {
    $component = EntityFormDisplay::load($id)->getComponent($component_id);
    $this->assertIsArray($component);
    $this->assertSame('comment_default', $component['type']);
    $this->assertSame(20, $component['weight']);
  }

  /** * Tests the migrated display configuration. */
  public function testMigration() {
    $this->assertDisplay('node.article.default', 'comment_node_article');
    $this->assertDisplay('node.company.default', 'comment_node_company');
    
parent::setUp();
    $this->migrateFields();
  }

  /** * Tests Drupal 6 upload settings to Drupal 8 entity form display migration. */
  public function testUploadEntityFormDisplay() {
    $this->executeMigration('d6_upload_entity_form_display');

    $display = EntityFormDisplay::load('node.page.default');
    $component = $display->getComponent('upload');
    $this->assertSame('file_generic', $component['type']);

    $display = EntityFormDisplay::load('node.story.default');
    $component = $display->getComponent('upload');
    $this->assertSame('file_generic', $component['type']);

    // Assure this doesn't exist.     $display = EntityFormDisplay::load('node.article.default');
    $component = $display->getComponent('upload');
    $this->assertNull($component);

    
public function __construct(ManufacturerServiceInterface $manufacturerService, Connection $connection)
    {
        $this->manufacturerService = $manufacturerService;
        $this->connection = $connection;
    }

    /** * {@inheritdoc} */
    public function supports(Element $element)
    {
        return $element->getComponent()->getType() === self::COMPONENT_NAME
            || $element->getComponent()->getConvertFunction() === self::LEGACY_CONVERT_FUNCTION;
    }

    /** * {@inheritdoc} */
    public function prepare(PrepareDataCollection $collection, Element $element, ShopContextInterface $context)
    {
    }

    /** * {@inheritdoc} */
/** * Asserts various aspects of a comment component in an entity view display. * * @param string $id * The entity ID. * @param string $component_id * The ID of the display component. * * @internal */
  protected function assertDisplay(string $id, string $component_id): void {
    $component = EntityViewDisplay::load($id)->getComponent($component_id);
    $this->assertIsArray($component);
    $this->assertSame('hidden', $component['label']);
    $this->assertSame('comment_default', $component['type']);
    $this->assertSame(20, $component['weight']);
  }

  /** * Tests the migrated display configuration. */
  public function testMigration() {
    $this->assertDisplay('node.page.default', 'comment_node_page');
    
'bundle' => 'entity_test',
      'mode' => 'default',
      'status' => TRUE,
      'content' => [
        'foo' => ['type' => 'visible'],
        'bar' => ['region' => 'hidden'],
        'name' => ['type' => 'hidden', 'region' => 'content'],
      ],
    ]);

    // Ensure that no region is set on the component.     $this->assertArrayNotHasKey('region', $entity_display->getComponent('foo'));

    // Ensure that a region is set on the component after saving.     $entity_display->save();

    // The component with a visible type has been assigned a region.     $component = $entity_display->getComponent('foo');
    $this->assertArrayHasKey('region', $component);
    $this->assertSame('content', $component['region']);

    $component = $entity_display->getComponent('bar');
    $this->assertArrayHasKey('region', $component);
    
$container->get('entity_field.manager')
    );
  }

  /** * {@inheritdoc} */
  protected function buildFieldRow(FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) {
    $field_row = parent::buildFieldRow($field_definition$form$form_state);

    $field_name = $field_definition->getName();
    $display_options = $this->entity->getComponent($field_name);

    // Insert the label column.     $label = [
      'label' => [
        '#type' => 'select',
        '#title' => $this->t('Label display for @title', ['@title' => $field_definition->getLabel()]),
        '#title_display' => 'invisible',
        '#options' => $this->getFieldLabelOptions(),
        '#default_value' => $display_options ? $display_options['label'] : 'above',
      ],
    ];

    
// Check the presence of expected cache tags.     $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', 'config:forum.settings');

    $this->drupalGet(Url::fromRoute('forum.page', ['taxonomy_term' => 1]));
    $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', 'config:forum.settings');

    // Do the admin tests.     $this->doAdminTests($this->adminUser);

    // Check display order.     $display = EntityViewDisplay::load('node.forum.default');
    $body = $display->getComponent('body');
    $comment = $display->getComponent('comment_forum');
    $taxonomy = $display->getComponent('taxonomy_forums');

    // Assert field order is body » taxonomy » comments.     $this->assertLessThan($body['weight']$taxonomy['weight']);
    $this->assertLessThan($comment['weight']$body['weight']);

    // Check form order.     $display = EntityFormDisplay::load('node.forum.default');
    $body = $display->getComponent('body');
    $comment = $display->getComponent('comment_forum');
    
// Check that the source field was created with the correct settings.     $storage = FieldStorageConfig::load("media.$field_name");
    $this->assertInstanceOf(FieldStorageConfig::class$storage);
    $field = FieldConfig::load("media.$type_name.$field_name");
    $this->assertInstanceOf(FieldConfig::class$field);
    $this->assertSame('mp3 wav aac', FieldConfig::load("media.$type_name.$field_name")->get('settings')['file_extensions']);

    // Check that the display holds the correct formatter configuration.     $display = EntityViewDisplay::load("media.$type_name.default");
    $this->assertInstanceOf(EntityViewDisplay::class$display);
    $formatter = $display->getComponent($field_name)['type'];
    $this->assertSame('file_audio', $formatter);

    // Create a media asset.     file_put_contents('public://file.mp3', str_repeat('t', 10));
    $file = File::create([
      'uri' => 'public://file.mp3',
      'filename' => 'file.mp3',
    ]);
    $file->save();

    $this->drupalGet("media/add/$type_name");
    


  /** * Tests that migrated view modes can be loaded using D8 API's. */
  public function testWidgetSettings() {
    // Test the config can be loaded.     $form_display = EntityFormDisplay::load('node.story.default');
    $this->assertNotNull($form_display);

    // Text field.     $component = $form_display->getComponent('field_test');
    $expected = [
      'type' => 'text_textfield',
      'weight' => 1,
      'region' => 'content',
      'settings' => [
        'size' => 60,
        'placeholder' => '',
      ],
      'third_party_settings' => [],
    ];
    $this->assertSame($expected$component, 'Text field settings are correct.');

    
$this->assertEquals(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, $field_storage->getCardinality());
    $this->assertEquals('preconfigured_storage_setting', $field_storage->getSetting('test_field_storage_setting'));

    $field = FieldConfig::loadByName('node', 'article', 'field_test_custom_options');
    $this->assertTrue($field->isRequired());
    $this->assertEquals('preconfigured_field_setting', $field->getSetting('test_field_setting'));

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

    $form_display = $display_repository->getFormDisplay('node', 'article');
    $this->assertEquals('test_field_widget_multiple', $form_display->getComponent('field_test_custom_options')['type']);
    $view_display = $display_repository->getViewDisplay('node', 'article');
    $this->assertEquals('field_test_multiple', $view_display->getComponent('field_test_custom_options')['type']);
    $this->assertEquals('altered dummy test string', $view_display->getComponent('field_test_custom_options')['settings']['test_formatter_setting_multiple']);
  }

  /** * Tests the access to non-existent field URLs. */
  public function testNonExistentFieldUrls() {
    $field_id = 'node.foo.bar';

    


  /** * {@inheritdoc} */
  public function getRenderer($field_name) {
    if (isset($this->plugins[$field_name])) {
      return $this->plugins[$field_name];
    }

    // Instantiate the formatter object from the stored display properties.     if (($configuration = $this->getComponent($field_name)) && isset($configuration['type']) && ($definition = $this->getFieldDefinition($field_name))) {
      $formatter = $this->pluginManager->getInstance([
        'field_definition' => $definition,
        'view_mode' => $this->originalMode,
        // No need to prepare, defaults have been merged in setComponent().         'prepare' => FALSE,
        'configuration' => $configuration,
      ]);
    }
    else {
      $formatter = NULL;
    }

    

  public function testForumMigration() {
    // Tests that the taxonomy_forums field storage config exists.     $field_storage_config = FieldStorageConfig::load('node.taxonomy_forums');
    $this->assertInstanceOf(FieldStorageConfig::class$field_storage_config);

    // Tests that the taxonomy_forums field config exists.     $field_config = FieldConfig::load('node.forum.taxonomy_forums');
    $this->assertInstanceOf(FieldConfig::class$field_config);

    // Tests that the taxonomy_forums entity view display component exists.     $entity_view_display = EntityViewDisplay::load('node.forum.default')->getComponent('taxonomy_forums');
    $this->assertIsArray($entity_view_display);

    // Tests that the taxonomy_forums entity form display component exists.     $entity_form_display = EntityFormDisplay::load('node.forum.default')->getComponent('taxonomy_forums');
    $this->assertIsArray($entity_form_display);

    // Test that the taxonomy_forums field has the right value.     $node = Node::load(19);
    $this->assertEquals(8, $node->taxonomy_forums->target_id);
  }

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