ADVERTISEMENT

Drupal - Programmatically do something when a node is updated

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) {
  // ...
}

The system will move to OO in Drupal 9.

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) {
  // ...
}

  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')));  

  }

Tags:

8

Hooks