function entity_test_entity_prepare_view

Same name and namespace in other branches
  1. 9 core/modules/system/tests/modules/entity_test/entity_test.module \entity_test_entity_prepare_view()
  2. 8.9.x core/modules/system/tests/modules/entity_test/entity_test.module \entity_test_entity_prepare_view()
  3. 10 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 700

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 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.