diff --git a/rules.routing.yml b/rules.routing.yml index ce2b86ce..8bff29fe 100755 --- a/rules.routing.yml +++ b/rules.routing.yml @@ -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' diff --git a/src/Controller/RulesReactionController.php b/src/Controller/RulesReactionController.php new file mode 100644 index 00000000..265fc27c --- /dev/null +++ b/src/Controller/RulesReactionController.php @@ -0,0 +1,37 @@ +$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'); + } + +} diff --git a/src/Entity/ReactionRuleConfig.php b/src/Entity/ReactionRuleConfig.php index 2047564f..dfbf21d8 100644 --- a/src/Entity/ReactionRuleConfig.php +++ b/src/Entity/ReactionRuleConfig.php @@ -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}" * } * ) diff --git a/src/EventSubscriber/GenericEventSubscriber.php b/src/EventSubscriber/GenericEventSubscriber.php index 290b3a9d..2476cb30 100644 --- a/src/EventSubscriber/GenericEventSubscriber.php +++ b/src/EventSubscriber/GenericEventSubscriber.php @@ -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) { diff --git a/src/Form/RulesComponentFormBase.php b/src/Form/RulesComponentFormBase.php index 590ea218..18c0210b 100644 --- a/src/Form/RulesComponentFormBase.php +++ b/src/Form/RulesComponentFormBase.php @@ -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); } diff --git a/tests/src/Functional/ConfigureAndExecuteTest.php b/tests/src/Functional/ConfigureAndExecuteTest.php index 2a99e062..89e8b203 100644 --- a/tests/src/Functional/ConfigureAndExecuteTest.php +++ b/tests/src/Functional/ConfigureAndExecuteTest.php @@ -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"!'); } } diff --git a/tests/src/Functional/UiPageTest.php b/tests/src/Functional/UiPageTest.php index cde6852b..8244401a 100644 --- a/tests/src/Functional/UiPageTest.php +++ b/tests/src/Functional/UiPageTest.php @@ -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. */