Skip to content

Issue #2702127 by yanniboi: Active / Inactive toggle for rules #449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: 8.x-3.x
Choose a base branch
from
Open
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: 4 additions & 2 deletions src/EventSubscriber/GenericEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ public function onRulesEvent(Event $event, $event_name) {
// variables added by one rule are not interfering with the variables of
// another rule.
foreach ($triggered_events as $triggered_event) {
// @todo Only load active reaction rules here.
$configs = $storage->loadByProperties(['events.*.event_name' => $triggered_event]);
$configs = $storage->loadByProperties([
'events.*.event_name' => $triggered_event,
'status' => 1,
]);

// Loop over all rules and execute them.
foreach ($configs as $config) {
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
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