I would like to perform the following mass action via VBO: send a mail to author of a node.

This mail is configured and sent in a hook_entity_update function.

The beginning of the function is the following :

if (is_null($entity)) return;
if (!isset($entity)) return;

$nid = \Drupal::routeMatch()->getRawParameter('node');
if ($nid) {
 $type = $node->bundle();
 if ($type != 'membre') {
else if ($nid == null) {


$mailManager->mail($module, $key, $to_trsf, $langcode, $params, NULL, $send);

test 1: If I update manually the node, the mail is sent.

test 2: If I update the node through a VBO, the mail is not sent because the nid is null, so the "return" statement is done.

Is there a possibility to retrieve the nid of a node when the hook is fired throught a VBO action?

1 Answer 1


Your problem is that you are using routeMatch() to get the node id, which won't be returned when using VBO.

You said you're using hook_entity_update():

function hook_ENTITY_TYPE_update(Drupal\Core\Entity\EntityInterface $entity)

This code assumes that the entity you are running hook_entity_update() for is the node entity.

Since the hook provides the $entity object, you can get the node id like this:

$nid = $entity->id()

  • Thank you for the solution!
    – cocq
    Feb 25, 2021 at 15:26
  • You're welcome. If the solution solves your problem, please click the checkbox to mark this answer as accepted. On this site, if you mark the question as accepted, you don't have to say thanks :) Feb 25, 2021 at 15:46
  • Yes, hooks should never presuppose they are only invoked from a specific page and retrieve values they need from the URL. The code of every route could save an entity, which means hook_entity_update() could be invoked from any route.
    – apaderno
    Feb 27, 2021 at 8:59

Your Answer

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

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