4

I have drupal 8, and I want to create a custom rule action. I have created it in Drupal 7, and it's working fine. I have no idea how to do it in Drupal 8.

Can you please explain which code should I use for Drupal 8?

Drupal 7 Code

function custom_rules_action_info() {
    return array(
        'custom_action1' => array(
            'label' => t('custom_action1'),
            'group' => t('Custom'),
            'parameter' => array(
                'user_id' => array(
                    'type' => 'integer',
                    'label' => t('User ID'),
                    'description' => t('User ID.'),
                ),
            ),
            'provides' => array(
                'return_value' => array(
                    'type' => 'text',
                    'label' => t('User Created with return_value'),
                ),
            ),
        ),
    );
}
1
  • 4
    I'm voting to close this question as off-topic because it's related to a feature currently being implemented in the Rules module, but not ready yet, and any answer will become out of date as soon as the issues are resolved Jun 20, 2016 at 13:59

1 Answer 1

6

Check the actions of the Rules module:

http://cgit.drupalcode.org/rules/tree/src/Plugin/RulesAction?h=8.x-3.x

These are the actions provided out of the box by Rules.

Each action class has a @RulesAction annotation. Wire that up and you should see your Rules action appear. Think of the annotation here very similar to what you would have in hook_rules_action_info().

The only exception is instead of defining it in the .module file, it will live in mymodule/src/Rules/Plugin/RulesAction/NameOfYourAction.php.

Here is an example of what it might look like:

namespace Drupal\mymodule\Plugin\RulesAction;

use Drupal\user\UserInterface;
use Drupal\rules\Core\RulesActionBase;

/**
 * Provides a 'custom action' action.
 *
 * @RulesAction(
 *   id = "mymodule_custom_action",
 *   label = @Translation("Custom Action"),
 *   category = @Translation("Custom"),
 *   context = {
 *     "user" = @ContextDefinition("entity:user",
 *       label = @Translation("User"),
 *       description = @Translation("Specifies the user we are taking action on.")
 *     ),
 *   }
 * )
 *
 */
class NameOfYourAction extends RulesActionBase {

  /**
   * Flag that indicates if the entity should be auto-saved later.
   *
   * @var bool
   */
  protected $saveLater = FALSE;

  /**
   * Does something to the user entity.
   *
   * @param \Drupal\user\UserInterface $account
   *   The user to take action on.
   */
  protected function doExecute(UserInterface $account) {
    // execution code
    // you may want to set $this->saveLater based on your use case
  }

  /**
   * {@inheritdoc}
   */
  public function autoSaveContext() {
    if ($this->saveLater) {
      return ['user'];
    }
    return [];
  }

}

Please note that at the time of this writing, Rules is in alpha release state. It may change with subsequent releases.

3
  • basicall it is Rule Action Plugin in Drupal 8 right. I will try your concept and implement. I hope it will work for me. I have created Drupal 7 way, but not visibile in Actions list.
    – AshwinP
    Jun 21, 2016 at 11:38
  • That won't work in Drupal 8 (the Drupal 7 way). Here is a very basic one I made a few months ago for my own module, which works. cgit.drupalcode.org/custom_pub/tree/src/Plugin/RulesAction/…
    – Kevin
    Jun 21, 2016 at 13:20
  • I think this might be in the right area of answering my question could I ask you to take a look? Oct 7, 2016 at 14:11

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