3

How can an action from the actions module be executed programatically? In D7 there was actions_do() but in D8 actions are plugins or configuration entities according to https://www.drupal.org/node/2020549.

4

You should inject the plugin manager as a service if possible, but for a plugin (code) based action:

$manager = \Drupal::service('plugin.manager.action');
$action = $manager->createInstance('action_plugin_id')
$action->execute();

Or if it's an advanced action, it's a config entity, so load and execute it that way:

$action = \Drupal\system\Entity\Action::load($action_id);
$action->execute();

Again if DI is an option, inject the entity type manager service and use that to load the action instead.

  • It's an advanced action I would like to execute that has configuration. It means I created an advanced action in config/system/actions. As far as I understand 'action_plugin_id' does not specify this advanced action? – Sebastian Jul 19 '18 at 11:05
  • All plugin instances (including actions) have an ID, it'll be in the name of the config file – Clive Jul 19 '18 at 11:22
  • I misunderstood @Sebastian, sorry, I thought you meant you'd added it via configuration. I edited the answer to cover your use case – Clive Jul 19 '18 at 12:36

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.