How to process the results of a VBO enabled view via Rules?

1
0
-1

How can a Drupal site builder use the power of a Views Bulk Operations (VBO) enabled view to process its views results via custom logic included in some custom Rules Actions and/or Rules Conditions? Whereas these Rules Actions and/or Rules Conditions are to be performed after some Rules Event occurred, and possible some (other) Rules Conditions must be met also?

And this by only using functionality that comes with either module, and without storing any PHP in the Drupal database?

Topics: 

Answer
1 answer

1
0
-1

Rules Actions

  • Use case: If something happens then perform a mass update on entities.
  • Details:

    Rules Events can be used to capture all sorts of things happening in a site. And, optionally, you can filter those events by adding Rules Conditions that must be satisfied. After a Rules Event occurred, and provided the optional Rules Conditions are satisfied, one or more Rules Actions will be performed.

    If VBO is enabled (and only then), you'll be able to specify the Rules Action "Load a list of entity objects from a VBO View". As part of its configuration, you can select any view that has a VBO field. This Rules Action will create a so called Rules List.

    The typical next (additional) Rules Action that is added, is a Rules Loop. Within this Rules Loop, each item in the Rules List (created from a VBO view) will get processed. And for the actual processing of each list item, you can use any of the typical Rules Actions.

  • How: A Rule with Rules Action "Load a list of entity objects from a VBO View" (+ Rules Loop).

  • Example: How to make menu badges count disappear after the page is viewed?
  • Demo: The video about Using VBO to load list of objects into Rules, starting from about 5:30 and up to 11:30 demonstrates this technique. Here is a summary of what is shown in this video (quote from the linked page):

    • You need a view that lists the entities you want to work on in Rules. Note that you don't need a display of the view – and it might even be better not to have one if you don't want the view to be displayed somewhere.
    • However, you need one bulk operations field. The type of bulk operations field determines what entity type will be sent to Rules. Note that you don't need any actions enabled for the field – it is enough that it is present.
    • You need a rule that, as an action, loads entities from a VBO. (This is available under the "Views bulk operations" group.) All view displayes with at least one VBO field will be selectable.
    • The action provides a list of entities, that can be used just like other lists in Rules. Combine with loops, actions and Rules Scheduler to make awesomeness happen.

Rules Conditions

VBO is not only useful within the context of Rules Actions, it can also be used for Rules Conditions.

Have a look at Niall Murphy's great answer to "How to have a field that counts the nodes submitted by the current user?".

Rules Conditions versus Views Filters

Assume you need to do some processing for a selected component type (e.g: only for Articles). You can use Rules Condition for that, or use a Views filter for it (as part of your VBO view).

Even though there is no best answer to decide about which one to use, here are some thoughts to help you decide:

  • Using a Views filter will minimize the number of rows to be processed (less IO).
  • Using a Rules Condition is like using a safety belt: it's an extra validation to ensure that your Rules Action will only be performed for the content type you had in mind when creating (testing) your Rule.
  • If in doubt, just use both ...

Happy Rules-processing with VBO!