Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions rules.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ entity.rules_reaction_rule.delete_form:
requirements:
_permission: 'administer rules+administer rules reactions'

entity.rules_reaction_rule.enable:
path: '/admin/config/workflow/rules/reactions/enable/{rules_reaction_rule}'
defaults:
_controller: '\Drupal\rules\Controller\RulesReactionController::performReactionRuleOperation'
op: 'enable'
requirements:
_permission: 'administer rules+administer rules reactions'
_csrf_token: 'TRUE'

entity.rules_reaction_rule.disable:
path: '/admin/config/workflow/rules/reactions/disable/{rules_reaction_rule}'
defaults:
_controller: '\Drupal\rules\Controller\RulesReactionController::performReactionRuleOperation'
op: 'disable'
requirements:
_permission: 'administer rules+administer rules reactions'
_csrf_token: 'TRUE'

### Rules Components
entity.rules_component.collection:
path: '/admin/config/workflow/rules/components'
Expand Down
37 changes: 37 additions & 0 deletions src/Controller/RulesReactionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Drupal\rules\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Config\Entity\ConfigEntityInterface;

/**
* Provides route controllers for Reaction Rules.
*/
class RulesReactionController extends ControllerBase {

/**
* Enables or disables a Rule.
*
* @param \Drupal\Core\Config\Entity\ConfigEntityInterface $rules_reaction_rule
* The rule entity.
* @param string $op
* The operation to perform, usually 'enable' or 'disable'.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* A redirect back to the rules list page.
*/
public function performReactionRuleOperation(ConfigEntityInterface $rules_reaction_rule, $op) {
$rules_reaction_rule->$op()->save();

if ($op == 'enable') {
drupal_set_message($this->t('The %label rule has been enabled.', ['%label' => $rules_reaction_rule->label()]));
}
elseif ($op == 'disable') {
drupal_set_message($this->t('The %label rule has been disabled.', ['%label' => $rules_reaction_rule->label()]));
}

return $this->redirect('entity.rules_reaction_rule.collection');
}

}
2 changes: 2 additions & 0 deletions src/Entity/ReactionRuleConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
* "collection" = "/admin/config/workflow/rules",
* "edit-form" = "/admin/config/workflow/rules/reactions/edit/{rules_reaction_rule}",
* "delete-form" = "/admin/config/workflow/rules/reactions/delete/{rules_reaction_rule}",
* "enable" = "/admin/config/workflow/rules/reactions/enable/{rules_reaction_rule}",
* "disable" = "/admin/config/workflow/rules/reactions/disable/{rules_reaction_rule}",
* "break-lock-form" = "/admin/config/workflow/rules/reactions/edit/break-lock/{rules_reaction_rule}"
* }
* )
Expand Down
6 changes: 6 additions & 0 deletions src/Form/RulesComponentFormBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ public function form(array $form, FormStateInterface $form_state) {
'#title' => $this->t('Description'),
];

$form['settings']['status'] = [
'#type' => 'checkbox',
'#default_value' => $this->entity->status(),
'#title' => $this->t('Active'),
];

return parent::form($form, $form_state);
}

Expand Down
11 changes: 11 additions & 0 deletions src/Plugin/RulesExpression/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ public static function create(ContainerInterface $container, array $configuratio
* {@inheritdoc}
*/
public function executeWithState(ExecutionStateInterface $state) {
// @TODO: If this solution is going to be implemented then this should be injected, but for now it works.
$storage = \Drupal::service('entity.manager')->getStorage('rules_reaction_rule');
// Check whether the reaction rule is enabled.
$rule_is_enabled = $storage->loadByProperties([
'expression.uuid' => $this->uuid,
'status' => 1,
]);
if (empty($rule_is_enabled)) {
// The reaction rule is disabled so do not run it.
return;
}
// Evaluate the rule's conditions.
if (!$this->conditions->isEmpty() && !$this->conditions->executeWithState($state)) {
// Do not run the actions if the conditions are not met.
Expand Down
10 changes: 10 additions & 0 deletions tests/src/Functional/ConfigureAndExecuteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ public function testConfigureAndExecute() {
$this->pressButton('Save');

$this->assertSession()->pageTextContains('Title matched "Test title"!');

// Disable rule and make sure it doesn't get triggered.
$this->drupalGet('admin/config/workflow/rules');
$this->clickLink('Disable');

$this->drupalGet('node/add/article');
$this->fillField('Title', 'Test title');
$this->pressButton('Save');

$this->assertSession()->pageTextNotContains('Title matched "Test title"!');
}

}
17 changes: 17 additions & 0 deletions tests/src/Functional/UiPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ public function testCancelExpressionInRule() {
$this->assertEquals(1, preg_match('#/admin/config/workflow/rules$#', $this->getSession()->getCurrentUrl()));
}

/**
* Tests that enabling and disabling a rule works.
*/
public function testRuleStatusOperations() {
// Setup an active rule.
$this->testCreateReactionRule();
$this->drupalGet('admin/config/workflow/rules');

// Test disabling.
$this->clickLink('Disable');
$this->assertSession()->pageTextContains('The Test rule rule has been disabled.');

// Test enabling.
$this->clickLink('Enable');
$this->assertSession()->pageTextContains('The Test rule rule has been enabled.');
}

/**
* Tests that deleting an expression from a rule works.
*/
Expand Down