function hook_entity_view

Act on entities being assembled before rendering.

Parameters

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

\Drupal\entity\Plugin\Core\Entity\EntityDisplay $display: The entity_display object holding the display options configured for the entity components.

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

$langcode: The language code used for rendering.

The module may add elements to $entity->content prior to rendering. The structure of $entity->content is a renderable array as expected by drupal_render().

See also

hook_entity_view_alter()

hook_comment_view()

hook_node_view()

hook_user_view()

Related topics

1 invocation of hook_entity_view()

File

drupal/core/includes/entity.api.php, line 325
Hooks provided the Entity module.

Code

function hook_entity_view(\Drupal\Core\Entity\EntityInterface $entity, \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display, $view_mode, $langcode) {

  // 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_field_extra_fields().
  if ($display
    ->getComponent('mymodule_addition')) {
    $entity->content['mymodule_addition'] = array(
      '#markup' => mymodule_addition($entity),
      '#theme' => 'mymodule_my_additional_field',
    );
  }
}