1

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.

  • "The website encountered an unexpected error. Please try again later." When developing, always enable verbose error output, so you can see what's wrong. – Berdir Sep 16 '18 at 17:00
  • I missed that it was your first post when I first looked at this. So WELCOME! And great follow up with edits to the original question and supplying an the final full answer to help others later. – LittleCoding Sep 16 '18 at 19:20
2

After researching more examples out there in the Web, I've finally managed to make this work, however I'm still not sure why it was not working before...

Working code:

<?php

/**
 * @file
 * Implementation of hook_entity_view_alter();
 */

function test_module_entity_view_alter($entity, $type, $view_mode) {
    drupal_set_message(t('Hello world!'));
}

Moving on to further modifications. Thanks everyone!

| improve this answer | |
0

Naming of your hook looks a little off. The format is 'MODULE_node_view_alter' and your module name is 'test_module'

Give 'test_module_node_view_alter' a try.

| improve this answer | |
  • I've changed the name, but the module is still not working. Updated original post with new code. – JustineHelfire Sep 16 '18 at 14:21
0

You need replace

function test_content_node_view_alter(array &$build, NodeInterface $node, EntityViewDisplayInterface $display, $view_mode) {
    drupal_set_message(t('Hello!'));
}

to

 /**
 * Implements hook_entity_view().
 */
  function test_module_entity_view(array &$build, NodeInterface $node, EntityViewDisplayInterface $display, $view_mode) {
        drupal_set_message(t('Hello!'));
  }

hook_node_view() it's invoked has just changed. As you mentioned, it's now handled by hook_ENTITY_TYPE_view(). or hook_entity_view()

| improve this answer | |
  • api.drupal.org/api/drupal/… is still an existing hook, so hook_node_view_alter() should work, if hook is replaced correctly. – Mario Steinitz Sep 16 '18 at 12:44
  • you change test_content_node_view_alter to test_module_node_view_alter – vinhdv Sep 16 '18 at 12:51
  • You should edit your code to follow Drupal's coding standards. – leymannx Sep 16 '18 at 12:53
  • I've updated the post. As @leymannx suggested, I've also reviewed coding standards and adjusted the code... Still no success. – JustineHelfire Sep 16 '18 at 14:22

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.