Drupal - What's hook_node_view() alternative?

hook_node_view() hasn't really been removed, the way it's invoked has just changed. As you mentioned, it's now handled by hook_ENTITY_TYPE_view().

So you can either use:

hook_entity_view()

or

hook_node_view()

The latter would make more sense if you're targeting nodes specifically.


Here is the example for hook_node_view in D8 version 8.1.1

function mymodule_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
   $build['body'][0]['#text'] = 'this is a new altered content body ';
}

Tags:

Nodes

8

Hooks