function FieldAttachOtherTest::testEntityDisplayBuild

Same name in other branches
  1. 8.9.x core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php \Drupal\Tests\field\Kernel\FieldAttachOtherTest::testEntityDisplayBuild()
  2. 10 core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php \Drupal\Tests\field\Kernel\FieldAttachOtherTest::testEntityDisplayBuild()
  3. 11.x core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php \Drupal\Tests\field\Kernel\FieldAttachOtherTest::testEntityDisplayBuild()

Tests rendering fields with EntityDisplay build().

File

core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php, line 28

Class

FieldAttachOtherTest
Tests other Field API functions.

Namespace

Drupal\Tests\field\Kernel

Code

public function testEntityDisplayBuild() {
    $this->createFieldWithStorage('_2');
    $entity_type = 'entity_test';
    $entity_init = $this->container
        ->get('entity_type.manager')
        ->getStorage($entity_type)
        ->create();
    // Populate values to be displayed.
    $values = $this->_generateTestFieldValues($this->fieldTestData->field_storage
        ->getCardinality());
    $entity_init->{$this->fieldTestData->field_name}
        ->setValue($values);
    $values_2 = $this->_generateTestFieldValues($this->fieldTestData->field_storage_2
        ->getCardinality());
    $entity_init->{$this->fieldTestData->field_name_2}
        ->setValue($values_2);
    // Simple formatter, label displayed.
    $entity = clone $entity_init;
    $display = \Drupal::service('entity_display.repository')->getViewDisplay($entity_type, $entity->bundle(), 'full');
    $formatter_setting = $this->randomMachineName();
    $display_options = [
        'label' => 'above',
        'type' => 'field_test_default',
        'settings' => [
            'test_formatter_setting' => $formatter_setting,
        ],
    ];
    $display->setComponent($this->fieldTestData->field_name, $display_options);
    $formatter_setting_2 = $this->randomMachineName();
    $display_options_2 = [
        'label' => 'above',
        'type' => 'field_test_default',
        'settings' => [
            'test_formatter_setting' => $formatter_setting_2,
        ],
    ];
    $display->setComponent($this->fieldTestData->field_name_2, $display_options_2);
    // View all fields.
    $content = $display->build($entity);
    $this->render($content);
    $this->assertRaw($this->fieldTestData->field
        ->getLabel(), "First field's label is displayed.");
    foreach ($values as $delta => $value) {
        $this->assertRaw("{$formatter_setting}|{$value['value']}", "Value {$delta} is displayed, formatter settings are applied.");
    }
    $this->assertRaw($this->fieldTestData->field_2
        ->getLabel(), "Second field's label is displayed.");
    foreach ($values_2 as $delta => $value) {
        $this->assertRaw("{$formatter_setting_2}|{$value['value']}", "Value {$delta} is displayed, formatter settings are applied.");
    }
    // Label hidden.
    $entity = clone $entity_init;
    $display_options['label'] = 'hidden';
    $display->setComponent($this->fieldTestData->field_name, $display_options);
    $content = $display->build($entity);
    $this->render($content);
    $this->assertNoRaw($this->fieldTestData->field
        ->getLabel(), "Hidden label: label is not displayed.");
    // Field hidden.
    $entity = clone $entity_init;
    $display->removeComponent($this->fieldTestData->field_name);
    $content = $display->build($entity);
    $this->render($content);
    $this->assertNoRaw($this->fieldTestData->field
        ->getLabel(), "Hidden field: label is not displayed.");
    foreach ($values as $delta => $value) {
        $this->assertNoRaw("{$formatter_setting}|{$value['value']}", "Hidden field: value {$delta} is not displayed.");
    }
    // Multiple formatter.
    $entity = clone $entity_init;
    $formatter_setting = $this->randomMachineName();
    $display->setComponent($this->fieldTestData->field_name, [
        'label' => 'above',
        'type' => 'field_test_multiple',
        'settings' => [
            'test_formatter_setting_multiple' => $formatter_setting,
        ],
    ]);
    $content = $display->build($entity);
    $this->render($content);
    $expected_output = $formatter_setting;
    foreach ($values as $delta => $value) {
        $expected_output .= "|{$delta}:{$value['value']}";
    }
    $this->assertRaw($expected_output, "Multiple formatter: all values are displayed, formatter settings are applied.");
    // Test a formatter that uses hook_field_formatter_prepare_view().
    $entity = clone $entity_init;
    $formatter_setting = $this->randomMachineName();
    $display->setComponent($this->fieldTestData->field_name, [
        'label' => 'above',
        'type' => 'field_test_with_prepare_view',
        'settings' => [
            'test_formatter_setting_additional' => $formatter_setting,
        ],
    ]);
    $content = $display->build($entity);
    $this->render($content);
    foreach ($values as $delta => $value) {
        $expected = $formatter_setting . '|' . $value['value'] . '|' . ($value['value'] + 1);
        $this->assertRaw($expected, "Value {$delta} is displayed, formatter settings are applied.");
    }
    // TODO:
    // - check display order with several fields
}

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