From 6410a1a00d34b47a6ba3c51f7cc4f138279f591d Mon Sep 17 00:00:00 2001 From: jonathan1055 Date: Fri, 21 Apr 2017 17:41:49 +0100 Subject: [PATCH] Issue #2702127 by jonathan1055, yanniboi: Active / Inactive toggle for rules --- rules.routing.yml | 18 +++++++++ src/Controller/RulesReactionController.php | 37 +++++++++++++++++++ src/Entity/ReactionRuleConfig.php | 2 + src/Form/RulesComponentFormBase.php | 6 +++ src/Plugin/RulesExpression/Rule.php | 11 ++++++ .../Functional/ConfigureAndExecuteTest.php | 10 +++++ tests/src/Functional/UiPageTest.php | 17 +++++++++ 7 files changed, 101 insertions(+) create mode 100644 src/Controller/RulesReactionController.php 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/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/src/Plugin/RulesExpression/Rule.php b/src/Plugin/RulesExpression/Rule.php index 9380fc43..499f6e61 100644 --- a/src/Plugin/RulesExpression/Rule.php +++ b/src/Plugin/RulesExpression/Rule.php @@ -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. 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. */