2

I have defined an action and want to trigger it from a hook.

We do not want to use the Rules module, because it is still buggy with Drupal 8. So, we cannot use rules_invoke_component().

What is the correct way to trigger an action? Do I need to construct a new Action object? Do I need a service? Is there documentation on that anywhere?

1
  • You must consider using the Business Rules module as it is a great alternative to Rules modules and it is quite stable for now. Commented Apr 23, 2019 at 3:07

2 Answers 2

3

This works:

$action = \Drupal::entityTypeManager()
  ->getStorage('action')
  ->load('send_mail');
if ($action) {
  $action->execute($entity);
}
2

You have to pass $entity in as an array - try

$action = \Drupal::entityTypeManager()
  ->getStorage('action')
  ->load('send_mail');
if ($action) {
  $action->execute([$entity]);
}
2
  • 1
    Without array, it worked perfectly for me. If it works with an array, too, I would find it better to mention this in a comment to the other answer. Having 2 answers with a nearly identical code snippet is confusing. Commented Aug 6, 2021 at 14:15
  • Hmmm... Passing a single entity on an 8.9 box gave me a whitescreen. The function signature looks for an array as its only argument, which is then passed to executeMultiple which sticks in a foreach. [
    – Deej
    Commented Aug 7, 2021 at 2:32

Your Answer

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

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