I'm hoping to finally find some solutions here. I'm new to custom module development in Drupal and I'm trying to learn the basics. What I'm trying to do is use hook_node_view_alter, however even at the simplest step (showing a status message) it does not work.
My module (test_module) is installed and enabled on the site. Currently the custom module has only two files: test_module.info.yml and test_module.module
Code from test_module.info.yml file
name: test_module
type: module
description: Test module
core: '8.x'
Code from test_module.module file
<?php
/*
* Implementation of hook_node_view_alter
*/
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\NodeInterface;
function test_content_node_view_alter(array &$build, NodeInterface $node, EntityViewDisplayInterface $display, $view_mode) {
drupal_set_message(t('Hello!'));
}
?>
It doesn't show the message when I'm opening a node (/node/1) at my Drupal site. As I understand it, it should show the message for any existing node when it is viewed. I've cleared the cache, nothing's changed.
Update
- changed test_content_node_view_alter() to test_module_node_view_alter()
- adjusted the code to coding standards
- removed the php closing tag
My code in test_module.module looks like this right now:
<?php
/**
* @file
* Implementation of hook_node_view_alter();
*/
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\NodeInterface;
function test_module_node_view_alter(array &$build, NodeInterface $node, EntityViewDisplayInterface $display, $view_mode) {
drupal_set_message(t('Hello!'));
}
I've cleared all cache and now the website gets this error:
The website encountered an unexpected error. Please try again later.