function FieldAttachOtherTest::testEntityDisplayBuild in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/field/src/Tests/FieldAttachOtherTest.php \Drupal\field\Tests\FieldAttachOtherTest::testEntityDisplayBuild()
Test rendering fields with EntityDisplay build().
File
- core/
modules/ field/ src/ Tests/ FieldAttachOtherTest.php, line 30 - Contains \Drupal\field\Tests\FieldAttachOtherTest.
Class
- FieldAttachOtherTest
- Tests other Field API functions.
Namespace
Drupal\field\TestsCode
function testEntityDisplayBuild() {
$this
->createFieldWithStorage('_2');
$entity_type = 'entity_test';
$entity_init = entity_create($entity_type);
// 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 = entity_get_display($entity_type, $entity
->bundle(), 'full');
$formatter_setting = $this
->randomMachineName();
$display_options = array(
'label' => 'above',
'type' => 'field_test_default',
'settings' => array(
'test_formatter_setting' => $formatter_setting,
),
);
$display
->setComponent($this->fieldTestData->field_name, $display_options);
$formatter_setting_2 = $this
->randomMachineName();
$display_options_2 = array(
'label' => 'above',
'type' => 'field_test_default',
'settings' => array(
'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, array(
'label' => 'above',
'type' => 'field_test_multiple',
'settings' => array(
'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, array(
'label' => 'above',
'type' => 'field_test_with_prepare_view',
'settings' => array(
'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
}