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

Alter the results of the entity build array for a particular entity type.

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 the Default theme implementations topic and \Drupal\Core\Render\RendererInterface::render() for details.

Parameters

array &$build: A renderable array representing the entity content.

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

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

See also

hook_ENTITY_TYPE_view()

hook_entity_view_alter()

Related topics

13 functions implement hook_ENTITY_TYPE_view_alter()

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

comment_node_view_alter in core/modules/comment/comment.module
Implements hook_ENTITY_TYPE_view_alter() for node entities.
content_translation_language_fallback_candidates_entity_view_alter in core/modules/content_translation/content_translation.module
Implements hook_language_fallback_candidates_OPERATION_alter().
contextual_contextual_links_view_alter in core/modules/contextual/contextual.module
Implements hook_contextual_links_view_alter().
contextual_test_block_view_alter in core/modules/contextual/tests/modules/contextual_test/contextual_test.module
Implements hook_block_view_alter().
contextual_test_contextual_links_view_alter in core/modules/contextual/tests/modules/contextual_test/contextual_test.module
Implements hook_contextual_links_view_alter().

... See full list

File

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

Code

function hook_ENTITY_TYPE_view_alter(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $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';
  }
}