function hook_entity_view

Same name in other branches
  1. 7.x modules/system/system.api.php \hook_entity_view()
  2. 9 core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_view()
  3. 10 core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_view()
  4. 11.x core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_view()

Act on entities being assembled before rendering.

Parameters

&$build: A renderable array representing the entity content. The module may add elements to $build prior to rendering. The structure of $build is a renderable array as expected by \Drupal\Core\Render\RendererInterface::render().

\Drupal\Core\Entity\EntityInterface $entity: The entity object.

\Drupal\Core\Entity\Display\EntityViewDisplayInterface $display: The entity view display holding the display options configured for the entity components.

$view_mode: The view mode the entity is rendered in.

See also

hook_entity_view_alter()

hook_ENTITY_TYPE_view()

Related topics

2 functions implement hook_entity_view()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

comment_entity_view in core/modules/comment/comment.module
Implements hook_entity_view().
content_moderation_entity_view in core/modules/content_moderation/content_moderation.module
Implements hook_entity_view().
1 invocation of hook_entity_view()
EntityViewBuilder::buildMultiple in core/lib/Drupal/Core/Entity/EntityViewBuilder.php
Builds multiple entities' views; augments entity defaults.

File

core/lib/Drupal/Core/Entity/entity.api.php, line 1482

Code

function hook_entity_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
    // Only do the extra work if the component is configured to be displayed.
    // This assumes a 'mymodule_addition' extra field has been defined for the
    // entity bundle in hook_entity_extra_field_info().
    if ($display->getComponent('mymodule_addition')) {
        $build['mymodule_addition'] = [
            '#markup' => mymodule_addition($entity),
            '#theme' => 'mymodule_my_additional_field',
        ];
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.