6

I want to do something when a node gets updated. To keep it simple, lets say display a drupal message.

hello_world.routing.yml

hello_world:
    path: /node/{node}
    defaults:
        _controller: Drupal\hello_world\Controller\HelloWorldController::hello_world_entity_update
    requirements:
        _permission: 'access content'

Not sure if the path should be /node/{node}/edit instead.

HelloWorldController.php

<?php
/**
 * @file
 * Contains \Drupal\hello_world\Controller\HelloController.
 */

namespace Drupal\hello_world\Controller;

use Drupal\Core\Controller\ControllerBase;

class HelloWorldController extends ControllerBase {

  public function hello_world_entity_update(Drupal\Core\Entity\EntityInterface $entity) { 
     
    drupal_set_message(t('Something @var just happened.', array('@var' => 'cool')));  
      
  }
}

Since hook_node_update() was depreciated, I assume we now have to use hook_entity_update().

I am not getting the message upon updating a node, what I'm I missing or doing wrong?


Update

Just so I don't confuse anyone. I was brainwashed into thinking I needed a routing file and a controller. All the D8 examples out there use them.

Since I was not creating any pages, turns out I did not need a routing.yml file nor a Controller.php file nor the /src folder. All I had to do is approach this like Drupal 7, all that I needed was the .info.yml file and a .module file to put the hook.

2
  • 1
    You have to put hook_entity_update() in helloword.module. This does not work as a controller.
    – 4uk4
    Commented May 10, 2016 at 9:22
  • Thank you for the disclaimer at the end. I'm a D7 veteran but new to D8 and I'm still trying to figure out when to use the OOP structure... all the documentation says how to use it, but not when...
    – neuquen
    Commented Feb 9, 2019 at 20:42

3 Answers 3

13

Hooks are still implemented as global, procedural functions; you need to declare them in the .module file just like Drupal 7, not in a controller class:

function MYMODULE_entity_update(Drupal\Core\Entity\EntityInterface $entity) {
  // ...
}

It's also worth noting that hook_ENTITY_TYPE_update() also exists, so you can still use hook_node_update():

function MYMODULE_node_update(Drupal\node\NodeInterface $node) {
  // ...
}
11
  • interesting, then what should I put in my controller? controllers are not mandatory?
    – No Sssweat
    Commented May 10, 2016 at 9:30
  • 1
    Typically a controller is linked up to a route, so they're generally used for building the markup (render array) for a page
    – Clive
    Commented May 10, 2016 at 9:32
  • so they're generally used for building the markup (render array) for a page since I am not building a page, leave the controller class empty? or delete it all together?
    – No Sssweat
    Commented May 10, 2016 at 9:34
  • Are you thinking a module requires a controller? It doesn't, so unless you need it for something (essentially building a page), just remove the file
    – Clive
    Commented May 10, 2016 at 9:35
  • according to this I need to have something for defaults: in my hello_world.rounting.yml, so what should I use instead? _entity_form?
    – No Sssweat
    Commented May 10, 2016 at 9:47
3
  1. You need not define any controller for this .
  2. Write this code in your custom .module file.

use Drupal\Core\Entity\EntityInterface;

 function module_name_entity_update(EntityInterface $entity) { 

    drupal_set_message(t('Something @var just happened.', array('@var' => 'cool')));  

  }
1
  • Does this hook get invoked when ie. a comment is added with ajax ie. ajax_comments module which uses ajax to add comments.
    – Sigma
    Commented Mar 1, 2019 at 6:52
-2

There are lots of hooks are available for node.

You can inherit in template.php file and write down your code.

you can use hook_node_update in template.php

1
  • hooks should be used in .module files and not in template.php. Maybe you're confusing them with function template_something. Ex: template_preprocess_node which goes in your template.php file.
    – No Sssweat
    Commented May 11, 2016 at 3:13

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.