0

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') {
   return;
 }
}
else if ($nid == null) {
 return;
}

...

$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?

3

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()

3
  • Thank you for the solution! – cocq Feb 25 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 :) – Patrick Kenny Feb 25 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. – kiamlaluno Feb 27 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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