function EntityViewBuilderTest::testViewField

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityViewBuilderTest.php \Drupal\KernelTests\Core\Entity\EntityViewBuilderTest::testViewField()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityViewBuilderTest.php \Drupal\KernelTests\Core\Entity\EntityViewBuilderTest::testViewField()
  3. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityViewBuilderTest.php \Drupal\KernelTests\Core\Entity\EntityViewBuilderTest::testViewField()

Tests EntityViewBuilder::viewField() language awareness.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityViewBuilderTest.php, line 215

Class

EntityViewBuilderTest
Tests the entity view builder.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testViewField() : void {
    // Allow access to view translations as well.
    Role::load(RoleInterface::ANONYMOUS_ID)->grantPermission('view test entity translations')
        ->save();
    $this->enableModules([
        'language',
        'content_translation',
    ]);
    $this->installEntitySchema('entity_test_mul');
    $en = ConfigurableLanguage::createFromLangcode('en');
    $en->save();
    $es = ConfigurableLanguage::createFromLangcode('es');
    $es->save();
    $this->container
        ->get('content_translation.manager')
        ->setEnabled('entity_test_mul', 'entity_test_mul', TRUE);
    $this->createEntityReferenceField('entity_test_mul', 'entity_test_mul', 'reference_field', 'Reference', 'entity_test_mul');
    // Make the entity reference field non-translatable to confirm it still
    // renders the correct language when displayed as an entity reference.
    $field = FieldConfig::loadByName('entity_test_mul', 'entity_test_mul', 'reference_field');
    $field->set('translatable', FALSE)
        ->save();
    // Create fields and displays for the test entity.
    FieldStorageConfig::create([
        'field_name' => 'text',
        'entity_type' => 'entity_test_mul',
        'type' => 'string',
    ])->save();
    FieldConfig::create([
        'field_name' => 'text',
        'entity_type' => 'entity_test_mul',
        'bundle' => 'entity_test_mul',
        'label' => 'Translated text',
        'translatable' => TRUE,
    ])->save();
    EntityViewMode::create([
        'id' => 'entity_test_mul.full',
        'targetEntityType' => 'entity_test_mul',
        'status' => FALSE,
        'enabled' => TRUE,
        'label' => 'Full',
    ])->save();
    $display = EntityViewDisplay::create([
        'targetEntityType' => 'entity_test_mul',
        'bundle' => 'entity_test_mul',
        'mode' => 'full',
        'label' => 'My view mode',
        'status' => TRUE,
    ])->setComponent('reference_field', [
        'type' => 'entity_reference_entity_view',
        'settings' => [
            'view_mode' => 'full',
        ],
    ])
        ->setComponent('text', [
        'type' => 'string',
        'region' => 'content',
    ]);
    $display->save();
    // Create the entity that will be displayed in the entity reference field
    // of the main entity.
    $referenced_entity = $this->createTestEntity('entity_test_mul');
    $referenced_entity->addTranslation('es', $referenced_entity->getTranslation('en')
        ->toArray());
    $referenced_entity->set('text', 'Text in English');
    $referenced_entity->getTranslation('es')->text = 'Text in Spanish';
    // The entity that will reference $referenced_entity.
    $main_entity = $this->createTestEntity('entity_test_mul');
    $main_entity->addTranslation('es', $main_entity->getTranslation('en')
        ->toArray());
    $main_entity->set('reference_field', $referenced_entity);
    $view_builder = $this->container
        ->get('entity_type.manager')
        ->getViewBuilder('entity_test_mul');
    $renderer = $this->container
        ->get('renderer');
    // Build the view for the reference field and render in English - the site
    // default. Confirm the reference field shows the content of the English
    // translation.
    $reference_field = $main_entity->get('reference_field');
    $reference_field_array_english = $view_builder->viewField($reference_field, 'full');
    $rendered_reference_field_english = $renderer->renderRoot($reference_field_array_english);
    $this->assertStringContainsString('Text in English', (string) $rendered_reference_field_english);
    // Change the default language to Spanish and render the reference
    // field again. It should display the contents of the Spanish translation.
    \Drupal::service('language.default')->set($es);
    \Drupal::languageManager()->reset();
    \Drupal::languageManager()->getCurrentLanguage();
    $reference_field_array_spanish = $view_builder->viewField($reference_field, 'full');
    $rendered_reference_field_spanish = $renderer->renderRoot($reference_field_array_spanish);
    $this->assertStringContainsString('Text in Spanish', (string) $rendered_reference_field_spanish);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.