function DisplayApiTest::testFieldItemView
Same name in other branches
- 8.9.x core/modules/field/tests/src/Kernel/DisplayApiTest.php \Drupal\Tests\field\Kernel\DisplayApiTest::testFieldItemView()
- 10 core/modules/field/tests/src/Kernel/DisplayApiTest.php \Drupal\Tests\field\Kernel\DisplayApiTest::testFieldItemView()
- 11.x core/modules/field/tests/src/Kernel/DisplayApiTest.php \Drupal\Tests\field\Kernel\DisplayApiTest::testFieldItemView()
Tests the FieldItemInterface::view() method.
File
-
core/
modules/ field/ tests/ src/ Kernel/ DisplayApiTest.php, line 228
Class
- DisplayApiTest
- Tests the field display API.
Namespace
Drupal\Tests\field\KernelCode
public function testFieldItemView() {
// No display settings: check that default display settings are used.
$settings = \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings('field_test_default');
$setting = $settings['test_formatter_setting'];
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->fieldName}[$delta];
$build = $item->view();
$this->render($build);
$this->assertText($setting . '|' . $value['value'], new FormattableMarkup('Value @delta was displayed with expected setting.', [
'@delta' => $delta,
]));
}
// Check that explicit display settings are used.
$display = [
'type' => 'field_test_multiple',
'settings' => [
'test_formatter_setting_multiple' => $this->randomMachineName(),
],
];
$setting = $display['settings']['test_formatter_setting_multiple'];
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->fieldName}[$delta];
$build = $item->view($display);
$this->render($build);
$this->assertText($setting . '|0:' . $value['value'], new FormattableMarkup('Value @delta was displayed with expected setting.', [
'@delta' => $delta,
]));
}
// Check that prepare_view steps are invoked.
$display = [
'type' => 'field_test_with_prepare_view',
'settings' => [
'test_formatter_setting_additional' => $this->randomMachineName(),
],
];
$setting = $display['settings']['test_formatter_setting_additional'];
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->fieldName}[$delta];
$build = $item->view($display);
$this->render($build);
$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 field are used.
$setting = $this->displayOptions['teaser']['settings']['test_formatter_setting'];
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->fieldName}[$delta];
$build = $item->view('teaser');
$this->render($build);
$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.
$setting = $this->displayOptions['default']['settings']['test_formatter_setting'];
foreach ($this->values as $delta => $value) {
$item = $this->entity->{$this->fieldName}[$delta];
$build = $item->view('unknown_view_mode');
$this->render($build);
$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.