drupal-7drupal-viewsdrupal-rules

How to trigger rule A before or after executing rule B?


I have a view displaying content. I'm using VBO (Views Bulk Operations) for selecting a set of rows from the view and perform some bulk operation. That operation is executing a Rule component in which the actual operation to be performed is provided as rule action.

But, I want to do some PHP action before and after executing the above Rule component. Is there a way to do it? Using views?


Solution

  • As I didn't find any other solution, I hacked the views_bulk_operations.module to accomplish my work.

    In views_bulk_operations_execute() function, add the code that you want to execute 'before' executing the rules component. In the foreach loop provided in the function, add your custom code.

    If you want to execute your code only once, then use the condition $current==2 inside foreach loop. If you want to execute your code only for one specific view, get the current view path into $token variable and compare it with your view path as given in the following code.

    foreach ($selection as $row_index => $entity_id) {
    $rows[$row_index] = array(
      'entity_id' => $entity_id,
      'views_row' => array(),
      // Some operations rely on knowing the position of the current item
      // in the execution set (because of specific things that need to be done
      // at the beginning or the end of the set).
      'position' => array(
        'current' => $current++,
        'total' => count($selection),
      ),
    );
    
    //Custom Code starts
    $token = strtok(request_path(),"/"); // Path of the view
    if($current==2 && $token == '<view path>') // Execute only once for the specified view
    {
        /* Your code that is to be executed before executing the rule component */
    }
    // Custom Code ends
    
    // Some operations require full selected rows.
    if ($operation->needsRows()) {
      $rows[$row_index]['views_row'] = $vbo->view->result[$row_index];
    }
    }
    

    In views_bulk_operations_execute_finished() function, add the code that you want to execute 'after' executing the rules component just above the last line _views_bulk_operations_log($message);.