function entity_test_entity_prepare_view

Same name in other branches
  1. 9 core/modules/system/tests/modules/entity_test/entity_test.module \entity_test_entity_prepare_view()
  2. 10 core/modules/system/tests/modules/entity_test/entity_test.module \entity_test_entity_prepare_view()
  3. 11.x core/modules/system/tests/modules/entity_test/entity_test.module \entity_test_entity_prepare_view()

Implements hook_entity_prepare_view().

File

core/modules/system/tests/modules/entity_test/entity_test.module, line 778

Code

function entity_test_entity_prepare_view($entity_type, array $entities, array $displays) {
    if ($entity_type == 'entity_test') {
        foreach ($entities as $entity) {
            
            /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
            // Add a dummy field item attribute on field_test_text if it exists.
            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',
                    ];
                }
            }
            // Add a dummy field 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.