Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/Entity/entity.api.php \hook_ENTITY_TYPE_view()
  2. 9 core/lib/Drupal/Core/Entity/entity.api.php \hook_ENTITY_TYPE_view()

Act on entities of a particular type 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_TYPE_view_alter()

hook_entity_view()

Related topics

18 functions implement hook_ENTITY_TYPE_view()

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

block_content_test_block_content_view in core/modules/block_content/tests/modules/block_content_test/block_content_test.module
Implements hook_block_content_view().
book_node_view in core/modules/book/book.module
Implements hook_ENTITY_TYPE_view() for node entities.
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().
entity_test_entity_prepare_view in core/modules/system/tests/modules/entity_test/entity_test.module
Implements hook_entity_prepare_view().

... See full list

File

core/lib/Drupal/Core/Entity/entity.api.php, line 1551
Hooks and documentation related to entities.

Code

function hook_ENTITY_TYPE_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 'my_module_addition' extra field has been defined for the
  // entity bundle in hook_entity_extra_field_info().
  if ($display
    ->getComponent('my_module_addition')) {
    $build['my_module_addition'] = [
      '#markup' => my_module_addition($entity),
      '#theme' => 'my_module_my_additional_field',
    ];
  }
}