Skip to main content
4 of 7
added 319 characters in body
No Sssweat
  • 31.6k
  • 14
  • 55
  • 91

Programmatically do something when a node is updated

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. Since I was not creating any pages, turns out I did not need a rounting.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.

No Sssweat
  • 31.6k
  • 14
  • 55
  • 91