1

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?

  • \Drupal::service('plugin.manager.action')->createInstance('foo', $configuration)->execute($data); – user21641 Sep 21 '17 at 11:06
  • Thank you, @IvanJaros. However, I didn't get your solution working. Maybe because I passed the wrong arguments to the functions. I've posted a solution that works as a community wiki. Feel free to improve it. – Philipp Zedler Sep 21 '17 at 15:12
  • 1
    Actions are like blocks. There is action plugin and action entity. Action entity only encapsulates configuration and action plugin id. So if you have existing action entity(ie. with configuration) you can simply call \Drupal\system\Entity\Action::load('foo')->execute($data); if you want to trigger the action without configuration or with different configuration, use the code above. – user21641 Sep 21 '17 at 15:19
1

This works:

$action = entity_load_multiple_by_properties(
  'action', array('id' => 'send_mail')
);

$action['send_mail']->execute([$entity]);
0

You must consider using the Business Rules module as it is a great alternative to Rules modules and it is quite stable for now.

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.