function hook_entity_view_alter

Same name in other branches
  1. 7.x modules/system/system.api.php \hook_entity_view_alter()
  2. 9 core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_view_alter()
  3. 8.9.x core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_view_alter()
  4. 11.x core/lib/Drupal/Core/Entity/entity.api.php \hook_entity_view_alter()

Alter the results of the entity build array.

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_view()

hook_ENTITY_TYPE_view_alter()

Related topics

4 functions implement 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.

content_translation_language_fallback_candidates_entity_view_alter in core/modules/content_translation/content_translation.module
Implements hook_language_fallback_candidates_OPERATION_alter().
field_layout_entity_view_alter in core/modules/field_layout/field_layout.module
Implements hook_entity_view_alter().
layout_builder_entity_view_alter in core/modules/layout_builder/layout_builder.module
Implements hook_entity_view_alter().
media_test_embed_entity_view_alter in core/modules/media/tests/modules/media_test_embed/media_test_embed.module
Implements hook_entity_view_alter().

File

core/lib/Drupal/Core/Entity/entity.api.php, line 1591

Code

function hook_entity_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.
        // The object must implement \Drupal\Core\Security\TrustedCallbackInterface.
        $build['#post_render'][] = '\\Drupal\\my_module\\NodeCallback::postRender';
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.