Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top