function DisplayApiTest::testFieldItemListView

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

Tests the FieldItemListInterface::view() method.

File

core/modules/field/tests/src/Kernel/DisplayApiTest.php, line 135

Class

DisplayApiTest
Tests the field display API.

Namespace

Drupal\Tests\field\Kernel

Code

public function testFieldItemListView() : void {
    $items = $this->entity
        ->get($this->fieldName);
    \Drupal::service('theme_installer')->install([
        'stark',
    ]);
    $this->config('system.theme')
        ->set('default', 'stark')
        ->save();
    // No display settings: check that default display settings are used.
    $build = $items->view();
    $this->render($build);
    $settings = \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings('field_test_default');
    $setting = $settings['test_formatter_setting'];
    $this->assertText($this->label, 'Label was displayed.');
    foreach ($this->values as $delta => $value) {
        $this->assertText($setting . '|' . $value['value'], new FormattableMarkup('Value @delta was displayed with expected setting.', [
            '@delta' => $delta,
        ]));
    }
    // Display settings: Check hidden field.
    $display = [
        'label' => 'hidden',
        'type' => 'field_test_multiple',
        'settings' => [
            'test_formatter_setting_multiple' => $this->randomMachineName(),
            'alter' => TRUE,
        ],
    ];
    $build = $items->view($display);
    $this->render($build);
    $setting = $display['settings']['test_formatter_setting_multiple'];
    $this->assertNoText($this->label, 'Label was not displayed.');
    $this->assertText('field_test_entity_display_build_alter', 'Alter fired, display passed.');
    $this->assertText('entity language is en', 'Language is placed onto the context.');
    $array = [];
    foreach ($this->values as $delta => $value) {
        $array[] = $delta . ':' . $value['value'];
    }
    $this->assertText($setting . '|' . implode('|', $array), 'Values were displayed with expected setting.');
    // Display settings: Check visually_hidden field.
    $display = [
        'label' => 'visually_hidden',
        'type' => 'field_test_multiple',
        'settings' => [
            'test_formatter_setting_multiple' => $this->randomMachineName(),
            'alter' => TRUE,
        ],
    ];
    $build = $items->view($display);
    $this->render($build);
    $setting = $display['settings']['test_formatter_setting_multiple'];
    $this->assertRaw('visually-hidden', 'Label was visually hidden.');
    $this->assertText('field_test_entity_display_build_alter', 'Alter fired, display passed.');
    $this->assertText('entity language is en', 'Language is placed onto the context.');
    $array = [];
    foreach ($this->values as $delta => $value) {
        $array[] = $delta . ':' . $value['value'];
    }
    $this->assertText($setting . '|' . implode('|', $array), 'Values were displayed with expected setting.');
    // Check the prepare_view steps are invoked.
    $display = [
        'label' => 'hidden',
        'type' => 'field_test_with_prepare_view',
        'settings' => [
            'test_formatter_setting_additional' => $this->randomMachineName(),
        ],
    ];
    $build = $items->view($display);
    $this->render($build);
    $setting = $display['settings']['test_formatter_setting_additional'];
    $this->assertNoText($this->label, 'Label was not displayed.');
    $this->assertNoText('field_test_entity_display_build_alter', 'Alter not fired.');
    foreach ($this->values as $delta => $value) {
        $this->assertText($setting . '|' . $value['value'] . '|' . ($value['value'] + 1), new FormattableMarkup('Value @delta was displayed with expected setting.', [
            '@delta' => $delta,
        ]));
    }
    // View mode: check that display settings specified in the display object
    // are used.
    $build = $items->view('teaser');
    $this->render($build);
    $setting = $this->displayOptions['teaser']['settings']['test_formatter_setting'];
    $this->assertText($this->label, 'Label was displayed.');
    foreach ($this->values as $delta => $value) {
        $this->assertText($setting . '|' . $value['value'], new FormattableMarkup('Value @delta was displayed with expected setting.', [
            '@delta' => $delta,
        ]));
    }
    // Unknown view mode: check that display settings for 'default' view mode
    // are used.
    $build = $items->view('unknown_view_mode');
    $this->render($build);
    $setting = $this->displayOptions['default']['settings']['test_formatter_setting'];
    $this->assertText($this->label, 'Label was displayed.');
    foreach ($this->values as $delta => $value) {
        $this->assertText($setting . '|' . $value['value'], new FormattableMarkup('Value @delta was displayed with expected setting.', [
            '@delta' => $delta,
        ]));
    }
}

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