I am building a module for Drupal 8 which needs to use hook_node_view(). I tried the following code


 * @file
 * Demonstrates the possibilities of forms in Drupal 8.

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\Node;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;

 * Implements hook_form_alter()
function demo_form_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  drupal_set_message(t('Found a form with ID %form_id', array('%form_id' => $form_id)));

 * Implements hook_node_view()
function demo_form_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
   drupal_set_message(t('its working'));

 * Implements hook_form_FORM_ID_alter().
 * Only alters the Search form 'search_block_form'.
function demo_form_form_search_block_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['hello'] = array(
    '#markup' => t('Go ahead, try me ...') . '<br />',
    '#weight' => -1,

I can't see any message from demo_form_node_view(). If the hook was correct, it should have shown the message. I am new to Drupal and I can't figure out why it is not working.

  • Did you clear your cache after adding that function? – Jaypan Jan 1 '17 at 1:22
  • I haven't found hook_node_view for Drupal 8 in api documentation. Hovewer, there is a hook_entity_view, which can be used instead hook_node_view. – Yakimkin Roman Jan 1 '17 at 6:29
  • The hook the OP is using is still invoked from Drupal. It is just documented under hook_ENTITY_TYPE_view(). – kiamlaluno Jan 1 '17 at 6:59
  • @YakimkinRoman I just found out that hook_entity_view is working... But i don't know why is this happening? hook_node is still supported in Drupal 8 AFAIK – Utkarsh Dixit Jan 1 '17 at 9:18
  • 1
    Try to set parameters for hook_node_view as (array &$build, NodeInterface $node, EntityViewDisplayInterface $display, $view_mode) If it works, I would be grateful for upvoting :) – Yakimkin Roman Jan 1 '17 at 9:23

In Drupal 8, the core Drupal hook hook_entity_view() is only called when the entity is "being assembled before rendering". See the documentation at https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!entity.api.php/function/hook_entity_view

After the entity has been rendered, core Drupal will cache the rendered entity and serve the rendered entity from the cache when needed. The entity is not rendered every time it is used. So BY DESIGN hook_entity_view() will not be called again when the rendered entity is loaded from the cache, it will only be called when the entity needs to be re-rendered - that is, when the entity is changed and/or the cache of that entity is invalidated.

This behavior is different from the way it worked in Drupal 7 because Drupal 7 did not cache rendered entities like this.

| improve this answer | |

This doesn't answer the question because the OP says he is building a module, but for others who stumble upon this question: I've found that this hook cannot be called from a theme. It must be called from a module. I could be wrong, but this has been my experience.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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