Php 钩子节点视图不工作
我正在为Drupal8构建一个模块,该模块需要使用Php 钩子节点视图不工作,php,drupal,drupal-8,Php,Drupal,Drupal 8,我正在为Drupal8构建一个模块,该模块需要使用hook\u node\u视图。我尝试了以下代码:- <?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\EntityViewDis
hook\u node\u视图
。我尝试了以下代码:-
<?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,
);
}
首先别忘了清除缓存。其次,请考虑一个实际的类型节点实体必须在页面中的某个地方使用,以便该钩子着火。因为您使用的是表单,所以可能没有加载节点 也有可能正在调用钩子,但由于重定向或清除消息(我对您的环境了解不够),drupal\u set\u消息无法工作。尝试
echo
something,然后退出代码>立即
最后,您可以尝试使用hook\u entity\u view
(这是[hook\u entity\u TYPE\u view][1]的更通用版本)
来查看您是否具有不同的效果。以防万一……您需要在添加新挂钩后清除Drupal缓存(例如drush cr
)。还可以尝试使用打印/回送行替换设置的消息行,以防调用钩子但消息因其他原因未显示。谢谢,我尝试了钩子实体视图,但它不起作用,8使用了回送并显示消息。