In Rules 7.x we had the ability to make a rule inactive, but this has not been ported to 8.x yet. I am in the process of converting the contrib Scheduler modules's actions, conditions and events to 8.x #2651348: Port Rules integration for Scheduler to Drupal 8. I have created many test rules, but when working in a branch of our code which does not have the Scheduler 8.x rules work-in-progress the test rules I have created cause fatal errors because the implementations do not exist.

I need to make the rules inactive, but keep them so I can return to the work later. I presume that if I disable the entire Rules module then any created rules will be deleted. I can't find any export functionality so I guess that has not been ported yet either. I don't want to loose all my work, but equally I can't continue without patching or re-implementing my rules development work.

Any ideas?

Jonathan
[maintainer of Scheduler]

Comments

jonathan1055 created an issue. See original summary.

dasjo’s picture

Thanks for posting this.

Not sure if still relevant, but regarding your question / use Case about keeping the rules for later work, you can already Export/import rules using CMI

jonathan1055’s picture

OK, I had not thought of that. Thanks for the hint. Yes I can see that I can export a single reaction rule, then delete the rule to allow a code switch to the non-rules branch, then re-import when I want to work on that branch of the code again. But I have created 15 separate reaction rules as part of testing my conversion from 7.x to 8.x so it is a great overhead in time to export all 15, delete them, then import all 15 later. And do that all again, if I need to switch branches to make a fix to the real code.

Are there any plans to implement the active/non-active toggle? If you would like it, but have not got round to looking at it, I could give it a try.

yanniboi’s picture

Issue summary: View changes
StatusFileSize
new17.87 KB
new19.29 KB
new39.72 KB
new35.47 KB

I've made a start of this on github here: https://github.com/fago/rules/pull/449

I have exposed the config entity operations for enable and disable by adding link templates and added a Controller to handle them.

I have also added the 'active' checkbox to reaction rule settings and made sure that the event subscriber only triggers active rules.

Screenshots attached (I also attached one screenshot of the settings from in d7 to show what the status checkbox looks like in d7).

Settings in D7

Settings in D8

jonathan1055’s picture

That looks brilliant. I've not got any account on github.com - what is the best way for me to test your changes? Can I extract them as a patch file? Or should I download your entire branch. I am currently on rule 8.x-3.0-alpha1+10-dev on my localhost, which is now a month out of date.

By the way, you confuse me when you say the screenshots are from D7 - this issue is only to fix D8, I think D7 version works just fine (?)

yanniboi’s picture

Hi @jonathan1055, github automatically generates a patch from a pull request if you add '.patch' to the end of the pull request url, so in this case https://github.com/fago/rules/pull/449.patch.

I can see you confusion about me mentioning drupal 7. The reason is that when you are porting changes to drupal 8 and no one told you want a field title or description should be, the best thing to do is what did it look like in d7, and that is what you should do in d8 until someone tells you that something different is better.

In d7 the rule status was a checkbox with 'Active' as the title and no description, so that is what I did for d8 and the screenshot shows this.

Thanks for helping review :)

yanniboi’s picture

Status: Active » Needs review
jonathan1055’s picture

Thanks for the explanation. Thank you also for giving the url for the patch - that is very good, and I can see that you have made another commit and it all gets wrapped up into the one patch.

I have tested it manually and found no problems so far. Changing the status via the checkbox and via the drop-down list both work correctly with no errors. Enabled rules are triggered and disabled rules are not triggered.

I tried running the tests which you changed, by selecting just \Drupal\Tests\rules\Functional\ConfigureAndExecuteTest and \Drupal\Tests\rules\Functional\UiPageTest on the testing UI page. However, this produced the error message "No test results to display". Not sure what is going on there - maybe these tests cannot be run in isolation?

Hope that helps - and thank you for providing this functionality so quickly after I raised the issue.

klausi’s picture

Status: Needs review » Needs work

Makes sense - the only thing I saw was the CSRF security vulnerability. Can you make sure those routes have the csrf protection enabled?

yanniboi’s picture

Status: Needs work » Needs review

Updated

jonathan1055’s picture

Status: Needs review » Needs work
StatusFileSize
new118.76 KB

I was about to test the minor change from #10 but unfortunately the combined patch no longer applies (was ok at alpha1+12, but not now at alpha1+17). The problem is GenericEventSubscriber.php which has changed right at the point where this patch added the 'status=1' criteria:

@@ -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,
+      ]);

Here is the code comparison:
https:GenericEventSubscriber changes between alpha1+12 and alpha1+17

I will revert to using alpha1+12 for now, as this toggle it vital for me, and it is not easy to see exactly how I should make the manual change for the failed hunk.

jonathan1055’s picture

Status: Needs work » Needs review
StatusFileSize
new6.51 KB

Following the reworking of GenericEventSubscriber onRulesEvent() as described above, I have finally worked out a way to filter for only enabled rules. I am sure this is not the best way, but it's a start. There is now no change to GenericEventSubscriber.php and the filtering is done in RulesExpression/Rule.php function executeWithState() instead. Apart from these two files, the other five in the patch have not changed.

I know that the entity.manager should be injected, but I am doing this just for 'proof of concept' or to see if others have better ways to do it. Also I realise that development has to take place on GitHub first. I am just submitting the patch to see how this goes and if I'm on the right lines.

Status: Needs review » Needs work

The last submitted patch, 12: 2702127-12.rules_.enable_disable_toggle.patch, failed testing.

jonathan1055’s picture

Well, I have no idea what happened there, but one nice thing ... the two modified tests to check the enable/disable functionality both passed OK.

The last submitted patch, 12: 2702127-12.rules_.enable_disable_toggle.patch, failed testing.

jonathan1055’s picture

Status: Needs work » Needs review
StatusFileSize
new322 bytes

Just to see if those test failures are anything to do with my patch, here is a dummy patch which changes nothing but a comment.

fago’s picture

Status: Needs review » Needs work

Yep, the tests are green in general. Could you create a PR for your patch according to https://github.com/fago/rules#contributing ? This will re-trigger a new test run on travis-ci also, what includes code style checks.

sibustephen’s picture

@fago, I will be looking into it.

tr’s picture

@sibustephen Thanks! This feature is already implemented in D8 Rules Essentials Please install that module and try it out. The code is an updated version of the patch by @jonathan1055 from #12. As far as I know it's fully working, but if you find problems please report them in the D8 Rules Essentials issue queue. There's also Drush support for this feature in #3035337: Port rules.drush.inc to D8 which I hope to commit to Rules in the next few days.

I put the enable/disable functionality in that project and not here because it's entangled in UI changes and improvements that are not easily separated. My intention is to get this and all the other features fully working in that project before moving those features into Rules.

Also, @fago isn't very active in this project currently. We're not using his github repository for development right now - we're doing everything through the issue queue here. So for this an all other Rules issues you should be posting patches / reviewing patches here in the issue queue and not on github.

sharique’s picture

@TR if it is working fine in tr_rulez, can we make a patch for this module?

tr’s picture

Issue tags: +D8RE

Tagging issues that will be fixed by code moved from D8RE.

tr’s picture

Status: Needs work » Fixed
jonathan1055’s picture

I was trying to make @yanniboi's code work back in 2016. Thank you @TR for all your work on this, and so many other Rules issues.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.