Act on entities being assembled before rendering.
\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().
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',
);
}
}