I'm trying to show a message for specific content type through a new module, on Drupal 8.3.0. (I'm a beginner.)

This is part of the code I am using.

File test_module.module (updated code, still not working)

 * @file
 * Implementation of hook_node_view();

function test_module_node_view($node, $view_mode) {
  if ($view_mode == 'full' && $node->type == 'test_content') {
      /*Message with current date and time*/
      $current_time = time();
      drupal_set_message(t('Hello! Today is '.format_date($current_time,'medium')));

As far as I know, this should work, but it doesn't and I don't know why. The message is not showing for a node of content type "test_content". How can I fix this? Is there another way to alter nodes of custom content types?



(Test_content_example is a node of type 'test_content', so there should be a system message displayed, but there's none.)

  • This doesn't work because rendered entities are cached. For logged in users you could set a message in an event subscriber. For anonymous users you need a client side solution (load the message in javascript).
    – 4uk4
    Commented Sep 17, 2018 at 6:53
  • 1
    Once the question is answered, its meaning cannot be changed. Since the answer says that $node->getType() should be used instead of $node->type, the code in the question cannot be changed to show you are using $node->getType().
    – apaderno
    Commented Sep 17, 2018 at 7:14

1 Answer 1


$node->type is a reference to the node type config entity - it's not a string.

You can use NodeInterface::getType() instead, e.g.:

if ($view_mode == 'full' && $node->getType() == 'test_content') {


Incidentally you could also use $node->bundle(), or even $node->type->entity->id(). But getType() is probably most appropriate for your specific needs.

You also have the function signature wrong - it should be:

function test_module_node_view(array &$build, \Drupal\node\NodeInterface $node, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode)
  • I'm using Drupal 8.3.0. I've changed the code a bit to illustrate the problem better. The message is not shown even when the condition is passed. Is the condition in if correct? Commented Sep 16, 2018 at 19:02
  • Oh ok, for D8 it's different. I've updated the answer
    – Clive
    Commented Sep 16, 2018 at 19:14
  • Ok, so I've tried all of your suggestions and sadly, none of them work. I've tried them before also, and after failing I decided to finally write here. I really don't see why it's not working... Updated the original post with your recommendation. Commented Sep 16, 2018 at 20:48
  • 1
    I just noticed the function signature...you're using the one from Drupal 7, you need to update it for the new version. I've added an example to the answer. Your phpdoc comments are actually in the Drupal 6 format as well, so whatever source you're using to put all this together you might want to consider looking for another one :)
    – Clive
    Commented Sep 16, 2018 at 21:56
  • Thinking about it, $node->getType() should have thrown you a fatal error with the current code. If it didn't, check the module is definitely installed
    – Clive
    Commented Sep 16, 2018 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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