function EntityTestHooks::entityPrepareView
Implements hook_entity_prepare_view().
File
-
core/
modules/ system/ tests/ modules/ entity_test/ src/ Hook/ EntityTestHooks.php, line 573
Class
- EntityTestHooks
- Hook implementations for entity_test.
Namespace
Drupal\entity_test\HookCode
public function entityPrepareView($entity_type, array $entities, array $displays) {
if ($entity_type == 'entity_test') {
foreach ($entities as $entity) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
// Add field item attributes on field_test_text if it exists.
// See \Drupal\Tests\system\Functional\Entity\EntityViewControllerTest::testFieldItemAttributes().
if ($entity->hasField('field_test_text') && $displays[$entity->bundle()]
->getComponent('field_test_text')) {
foreach ($entity->get('field_test_text') as $item) {
$item->_attributes += [
'data-field-item-attr' => 'foobar',
'property' => 'schema:text',
];
}
}
// Add an item attribute on daterange fields if they exist.
$fields = $entity->getFieldDefinitions();
foreach ($fields as $field) {
if ($field->getType() === 'daterange') {
$item = $entity->get($field->getName());
$item->_attributes += [
'data-field-item-attr' => 'foobar',
];
}
}
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.