9

I am using Drupal 8 Beta 14 and I want to edit the node object in teaser view mode. As hook_node_view is removed in Drupal 8. What's the alternative hook or some other way that I can use to edit the node? Is it hook_ENTITY_TYPE_view or hook_entity_view or some other thing?

Thank you.

2 Answers 2

22

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.

2
  • Thank you Clive, hook_node_view is picked. But I'm wondering where is it defined? I could find it only in NodeRSSContentTest.php file. Secondly hook_ENTITY_TYPE_view never gets called, no matter what I do. Why is it so?
    – rashidkhan
    Commented Sep 4, 2015 at 14:28
  • 2
    Because ENTITY_TYPE is a placeholder - it needs to be replaced with the actual entity type (so it would be hook_node_view for a node, hook_user_view for a user, hook_profile2_view for a profile2 entity, and so on)
    – Clive
    Commented Sep 4, 2015 at 14:32
10

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 ';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.