How to use Rules to create a custom bulk operation for a VBO view?


How can a Drupal site builder use the power of Rules (Conditions and Actions) to extend the list of available bulk operations of a Views Bulk Operations (VBO) enabled view with custom logic included in some custom rule?

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


1 answer

  • Use case:

    VBO comes, out of the box, with some predefined (hardcoded) operations that is to be applied to each row selected in the view. The available operations are shown with "Selected bulk operations" when configuring the VBO field. Some examples included are:

    • Delete item
    • Execute arbitrary PHP script
    • Modify entity values
    • Send e-mail
    • ...

    Even though they come in handy when you need such operation, these operations are rather basic / standard. If however you have some fancy logic (typically special conditions / actions that must be performed (and you don't want to, or cannot, use an arbitrary PHP script), then using Rules (Conditions and/or Actions) you might probably get it to work.

  • How: Execute a VBO operation which you define using a Rules Component.
  • Example: How to bulk update select list fields?
  • Demo: The video about Using Rules components with VBO" uses a Rules Component with (only) 1 parameter. If you want even more, then also watch the subsequent "Using Rules components with more than one parameter" video.
  • Attention: to process 100.000 nodes at once is quite a challenge (memory requirements, etc). Therefor you might want to also watch the video about "More details about VBO settings" ... Before you are tempted to use VBO for processing them all at once ...

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 VBO-processing with Rules!