I don't see a way to run a Rules component.
Also, I don't see a way to add parameters to a Rules component.

Comments

OnkelTem created an issue. See original summary.

OnkelTem’s picture

Title: How to run Rules Component? » How to run a Rules component?
Graber’s picture

Status: Active » Postponed (maintainer needs more info)

Please describe your problem with more details, links etc. And mind your language, that last sentence of the issue description is not really needed here, is it?

Graber’s picture

Title: How to run a Rules component? » Allow executing "action set" Rules components
Category: Support request » Feature request
Issue summary: View changes
Status: Postponed (maintainer needs more info) » Active
joelpittet’s picture

Category: Feature request » Support request
Status: Active » Postponed (maintainer needs more info)

This needs more details in the issue summary before we can help you.

Graber’s picture

In general, Rules for D8 is still in alpha phase, so its API may be subject to significant changes. I tried rising the issue on #2245015: [META] Rules 8.x Roadmap but without much interest from the Rules guys.

Integration can be performed by using the VBO API, subscribing to 'views_bulk_operations.action_definitions' event from Rules and providing action sets along with action class that will execute the sets, but it has to be done in the Rules module.

Feel free to rise an issue there with a link to this issue, if more people are interested in having this working we'll surely be able to break the ice.

TR’s picture

Rules for D8 is still in alpha phase, so its API may be subject to significant changes

No, it's not subject to significant changes. The alpha status of Rules is due to a number of missing features, especially related to the admin UI. The relevant issues are marked as "beta blocker" so they may be easily found. The core functionality and API is pretty much complete and stable - no API changes that I know of in the past year. Rules is quite usable IF you are willing to deal with problems caused by a minimal UI.

I tried rising the issue on #2245015: [META] Rules 8.x Roadmap but without much interest from the Rules guys.

Hmm, in that issue, your post (9 Jan 2018) said:

Hi all, nice to know there are such meetings, I will not be able to attend this one but on the next one it'll be good to raise the subject of Rules-VBO compatibility for executing action sets so I'll be there if nothing unexpected happens. VBO issue: #2931170: Allow executing "action set" Rules components

I *did* attend that meeting, and the one after it, and you didn't attend or "raise the subject of Rules-VBO compatibility". So it's not surprising that nothing was done. At the time you wrote #6, the second meeting hadn't even happened yet, so your post in the Rules issue queue saying you intended to raise the subject in the future was your only participation. Your post didn't ask for anyone to look at your issue, so I don't know what you expected in the way of a response. You simply posted a link to the issue without any comment. It hardly constitutes lack of interest by the "Rules guys".

Anyway, at this point it is certainly possible to port to D8 the conditions and actions that were provided by VBO in D7. There's even some documentation on how to port Rules events/conditions/actions, if you're interested in doing this - see https://www.drupal.org/docs/8/modules/d8-rules-essentials/for-developers . I am working on completing and improving that documentation, so if there is something wrong in there please correct it or if there is something missing (aside from the obvious placeholder pages) please let me know.

In the end, it's up to the VBO maintainer whether to port the Rules support from D7 to D8. It's certainly something I would encourage, and as one of the "Rules guys" I'm happy to answer questions about Rules in D8, but that functionality is part of VBO and needs to be done in VBO as it was in D7, or another contributed module, not Rules core.

joey-santiago’s picture

We run into this need, but found out a very inexpensive solution to this problem. Our rule is triggered when the content is saved and there's already a VBO operation that saves content. So triggering that operation also triggers our rule. Of course it would be "cooler" being able to directly run the action, but this requires no coding, so maybe it could be a nice workaround for other people as well.

joey-santiago’s picture

I've been playing with this and asked for guidance on https://www.drupal.org/project/rules/issues/3214944 too.

with a custom action code like this, it seems to be possible to run a rule component with an action:

<?php

namespace Drupal\trimble_custom_rules\Plugin\Action;

use Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionBase;
use Drupal\views_bulk_operations\Action\ViewsBulkOperationsPreconfigurationInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\rules\Entity\RulesComponentConfig;
use Drupal\rules\Context\ContextDefinition;

/**
 * Run a rule component.
 *
 * @Action(
 *   id = "run_rule_action",
 *   label = @Translation("Run a rule"),
 *   type = "node",
 *   confirm = TRUE,
 * )
 */
class RulesAction extends ViewsBulkOperationsActionBase implements ViewsBulkOperationsPreconfigurationInterface, PluginFormInterface {

  /**
   * {@inheritdoc}
   */
  public function execute($entity = NULL) {
    /*
     * All config resides in $this->configuration.
     * Passed view rows will be available in $this->context.
     * Data about the view used to select results and optionally
     * the batch context are available in $this->context or externally
     * through the public getContext() method.
     * The entire ViewExecutable object  with selected result
     * rows is available in $this->view or externally through
     * the public getView() method.
     */

    // Do checks.
    if (!isset($this->configuration['rule_config_setting'])) {
      return;
    }
    $rule = $this->configuration['rule_config_setting'];
    $rule = RulesComponentConfig::load($rule);
    $context = ContextDefinition::createFromArray(['@node.node_route_context:node' => $entity]);
    $rule->setContextDefinitions([$context]);
    $component = $rule->getComponent();
    $component->addContextDefinition('@node.node_route_context:node', $context);
    $component->provideContext('@node.node_route_context:node');
    $component->executeWithArguments(['@node.node_route_context:node' => $entity]);
  }

  /**
   * {@inheritdoc}
   */
  public function buildPreConfigurationForm(array $form, array $values, FormStateInterface $form_state) {
    $form['rule_pre_config_setting'] = [
      '#title' => $this->t('Rule to run'),
      '#type' => 'textfield',
      '#default_value' => isset($values['rule_pre_config_setting']) ? $values['rule_pre_config_setting'] : '',
    ];
    return $form;
  }

  /**
   * Configuration form builder.
   *
   * If this method has implementation, the action is
   * considered to be configurable.
   *
   * @param array $form
   *   Form array.
   * @param Drupal\Core\Form\FormStateInterface $form_state
   *   The form state object.
   *
   * @return array
   *   The configuration form.
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['rule_config_setting'] = [
      '#title' => t('Which rule do you want to run?'),
      '#type' => 'textfield',
      '#default_value' => isset($this->context['preconfiguration']['rule_pre_config_setting']) ? $this->context['preconfiguration']['rule_pre_config_setting'] : '',
    ];

    return $form;
  }

  /**
   * Submit handler for the action configuration form.
   *
   * If not implemented, the cleaned form values will be
   * passed direclty to the action $configuration parameter.
   *
   * @param array $form
   *   Form array.
   * @param Drupal\Core\Form\FormStateInterface $form_state
   *   The form state object.
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    // This is not required here, when this method is not defined,
    // form values are assigned to the action configuration by default.
    // This function is a must only when user input processing is needed.
    $this->configuration['rule_config_setting'] = $form_state->getValue('rule_config_setting');

  }

  /**
   * {@inheritdoc}
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
    if ($object->getEntityTypeId() === 'node') {
      $access = $object->access('edit', $account, TRUE);
      return $return_as_object ? $access : $access->isAllowed();
    }

    // Other entity types may have different
    // access methods and properties. For now, FALSE.
    return FALSE;
  }

}

for now i tested it only with simple rules actions. I'm afraid it won't work with more complex ones, that would require other parameters to be sent to them...

kopeboy’s picture

Thank you very much Joey.
What additional info the maintainer needs yet is not clear to me.
Like it was done in D7, now we need to be able to launch Rules Components with VBO.
I think we should change this issue to a Feature request for the D9 version.

Graber’s picture

Please see my comment #6 to this issue. It still applies unfortunately and years pass. No point working on APIs that are still subject to change and are not really supported by their maintainers. The info I need is when Rules for Drupal 8+ will have a stable release.

Of course anyone can try to work on this but IMO Rules itself is what needs work first, otherwise this’ll be a lost effort.