Background
Ok, so you like the way you can use Rules to create useful domino effects on your site. “When someone logs in, redirect to page X if she belongs to role Y.” “When someone creates an account, give her the noob role and then remove it four weeks later.” You name it, Rules does it.
And of course you like Views. (You’d be mad if you didn’t.) You have probably set up a number of Bulk Operation Views-styles to automate things like “demote from front page”, “unpublish comments with spam flags” and stuff like that.
Now, combine the two. Not the usual way, where you invoke Rule sets from VBO. No, you use Rules to call VBO, throw in some arguments and parameters, and execute the bulk operation straight down. No clicking by administrators, just domino stuff by Rules.
Cool, huh? I mean, that would be about 60 percent of what you need to take on the world.
Case
So, the situation is like this:
I have a number of master nodes, each of them with some 200+ sub nodes refering to them. When I switch a flag on the master node, I want flags on the sub nodes to be switched.
Step one is to build a handy VBO list, where I use the master node NID as an argument to list all the sub nodes belonging to it. In the VBO list I enable the operations to switch on and off flags, and just for the sake of it I also try it manually.
Step two is to build a rule triggered on the master flag. Among the actions the rule could launch there is the option Execute a VBO programatically (under the title Views Bulk Operations). This is the one.
In the settings for the action I select a VBO view and an operation, namely the ones I just built. Three textareas give me the chance to let Rules send along (1) operation arguments, (2) exposed filters inputs, and (3) view arguments. I want to use the third to pass along the master NID to VBO.
Finding the argument
This was where it got tricky. Rules says that the variables &$object and $context are available, but since it is a flag rather than a node action calling the rule there is no node object loaded.
The short way around this was to analyze the path for the flag, noticing that the third argument actually gives the NID of the node. Typing return arg(3); solved the problem – perhaps not in the most elegant way, but certainly in a very functional way. (Cred goes to dixon_ for finding this out!)
The result: Whenever someone activates a master flag, the VBO flagger is run on all its sub nodes. One click, 200+ flags. To make it complete there should of course also be a rule for unflagging set up in a similar way.
Conclusion
For me this is a great example on how much power you can get from Drupal without coding. Sure, you could write your own module for this specific task – but that means one more mini module to update when the site changes. Now it’s all in configuration of standard modules, even managable and deployable by Features.
I would have been happier if I could say “not a single line of code”, but I’ll settle for “one single line of code”. :-)
PS: I also did some experimenting with using Rules + VBO to send out e-mails to users. It was interesting indeed, alas not very rewarding.
