function hook_entity_view_alter

Alter the results of ENTITY_view().

This hook is called after the content has been assembled in a structured array and may be used for doing processing which requires that the complete entity content structure has been built.

If a module wishes to act on the rendered HTML of the entity rather than the structured content array, it may use this hook to add a #post_render callback. Alternatively, it could also implement hook_preprocess_HOOK() for the particular entity type template, if there is one (e.g., node.html.twig). See drupal_render() and theme() for details.

Parameters

$build: A renderable array representing the entity content.

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

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

See also

hook_entity_view()

hook_comment_view_alter()

hook_node_view_alter()

hook_taxonomy_term_view_alter()

hook_user_view_alter()

Related topics

1 function implements hook_entity_view_alter()

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

edit_entity_view_alter in drupal/core/modules/edit/edit.module
Implements hook_entity_view_alter().

File

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

Code

function hook_entity_view_alter(&$build, Drupal\Core\Entity\EntityInterface $entity, \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display) {
  if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {

    // Change its weight.
    $build['an_additional_field']['#weight'] = -10;

    // Add a #post_render callback to act on the rendered HTML of the entity.
    $build['#post_render'][] = 'my_module_node_post_render';
  }
}