Writing your own action plugin

Last updated on
6 November 2017

Action plugin can be implemented for domains, cores, hosts or for the global dashboard itself. DRD comes with a list of several dozen action plugin included, but you can easily write your own. All you need to do is in your DRD installation to install the latest version of DRD and also Drush 9-beta8 or later. Then follow these instructions:

1. Generate your action plugin

Drush 9 comes with a Drupal code generator and we have utilised that to quick-start your plugin development. Simply call

drush generate drd-action-plugin

and answer the questions:

So you need to tell for which of your modules you want to create an action plugin and then give your action an ID, a label and the type, i.e. in which scope this action can be executed. Drush's code generator will then automatically generate all the required files for you so that you can then immediately start writing your code.

2. Write your code

Before we start coding, let's have a look on what the code generator created:

In your custom module's (mymodule) directory you'll find new or appended files:

  • Module root: the 2 services files are for Drush and Drupal console, so by now you new action plugin is already known to both CLI tools that are available for Drupal.
  • config/optional: the config file declares your new action and you should import that into your current site with this command:
    drush cim --partial --source=/path/to/Drupal/modules/custom/mymodule/config/optional

    which is required only once for each new plugin so that Drupal core knows about the action entity.

  • src/Agent/Action: here you find the code which will be executed remotely in your domains, for each supported Drupal core version there is a separate file. All your code needs to be in that single file, you can actually load other customer file while executing an action remotely but you can call any available code remotely like from Drupal core.
  • src/Command: this is used by Drupal Console to execute your action. Nothing to be done here.
  • src/Commands: this is used by Drush 9 to execute your action. Nothing to be done here.
  • src/Plugin/Action: here is a class for your action which is used in DRD to handle the action, fire the remote request and act upon the result that you receive from the remote site.

So, there are only two places where you would want to write code: in src/Plugin/Action for everything that should happen in DRD and src/Agent/Action for everything that should happen remotely. That's about it.

3. Execute your action

Once your code is completed, you can execute the action plugin like any other action that ships with DRD. Here you see an example for the action plugin that we used to explain the action creation executed on a single domain with the id 128:

The Drush command is automatically named as drd:[modulename]:[action-id] and a similar pattern applies to the action name for Drupal Console.

 

Help improve this page

Page status: No known problems

You can: