4

I would like to execute certain Rules based bulk operations on a view (using the VBO module). But not the way they are normally done when using VBO, i.e selecting the rows in the view and then clicking the operation to execute.

Instead I want VBOs to get executed when the view page is displayed. So that I dont need to select the rows and execute VBO operations on them.

Any suggestions?

0

To "trigger the execution of certain Rules when visiting the view page" (as in your question), is something you can do by using a special feature of the Rules module, i.e. a technique that is similar to what is described in the answer to "How to execute VBO list of nodes in Rules on After updating an existing user account event?". Read on for more details about the steps to be completed, specific to your question here.

Create a custom Rules that will execute the VBO list that you already have, and which looks similar to this example (which was used to process a VBO view of unread messages, with a path like user/%/messages/unread):

{ "rules_mark_all_unread_messages_read" : {
    "LABEL" : "Mark all unread messages read",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "views_bulk_operations" ],
    "ON" : { "init" : [] },
    "IF" : [
      { "text_matches" : {
          "text" : [ "site:current-page:url" ],
          "match" : "user\/\\d+\/messages\/unread",
          "operation" : "regex"
        }
      }
    ],
    "DO" : [
      { "views_bulk_operations_action_load_list" : {
          "USING" : { "view" : "messages_by_user|page_3" },
          "PROVIDE" : { "entity_list" : { "unread_msgs_list" : "A list of unread messages" } }
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "unread-msgs-list" ] },
          "ITEM" : { "current_msg" : "Current message" },
          "DO" : [
            { "component_rules_flag_a_message" : { "message_to_flag" : [ "current-msg" ] } },
            { "drupal_message" : { "message" : "The message with ID [current-msg:mid] (created [current-msg:timestamp]) was marked as read." } }
          ]
        }
      }
    ]
  }
}

Some more details about the rule above:

  • Rules Event: Drupal is initializing
  • Rules Condition: Check if the path being visited is user/%/messages/unread (adapt this path to use your path).
  • Rules Actions:

    • Create a list of unflagged messages (replace this list by your VBO list).
    • Loop over each of the unflagged messages found, and flag those messages (replace this action by the "execution of certain rules", as in your question).

This rule also performs an extra "set message" action, so that you can see what it does if you want to try it in your own site (it shows a message for each entry in the list). Remove that message after QA-testing if you want.

That's it, works like a charm!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.