Reputation: 4275
I am building a module for drupal 8 which needs to use hook_node_view
. I tried the following code:-
<?php
/**
* @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 than it should have shown the message. I am new to drupal and can't figure out why demo_form_node_view
is not working.
Upvotes: 1
Views: 1163
Reputation: 4680
First of all don't forget to clear caches. Second of all, please consider that an actual entity of type node has to be used somewhere in the page for that hook to fire. Since you are working with a form maybe you're not loading a node?
It's also possible that the hook is being called but drupal_set_message
doesn't work because of a redirect or something clearing the messages (I don't know your environment enough). Try to echo
something and then exit;
immediately.
Finally you can try using hook_entity_view
(which is the more generic version of [hook_ENTITY_TYPE_view][1]
to see if you are a different effect.
Upvotes: 1