문제

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?

도움이 되었습니까?

해결책 2

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);.

다른 팁

Just insert the 2 actions you want to execute before and after the target action.

According to your issue since you have a VBO action there is a Variable:Parameter set. You should add 2 new Variables for the 2 actions. One as Parameter (before) and one as Provided (after) and then add the 2 actions taking the values from the action before.

If the Rules action set does not allow additional Variables you have to clone it.

I used a custom module to create my own action using hook_action_info(). You can use the guide on Drupal.org to help you create your custom action.

Part of creating your custom action involves declaring a callback function that accepts the row and a $context array, structured as following: Structure of context array passed into action callback

Using that progress element, you can therefore determine how far your your bulk execution is. So your code could look something like:

function my_module_action_my_operation(&$row, $context = array()) {
  // Stuff to do before we start with the list
  if($context['progress']['current'] == 1) {
    do_pre_processing_stuff_here();
  }

  // Programmatically call the Rule component here. 
  // Do any other operations per $row

  if($context['progress']['current'] == $context['progress']['total']) {
    do_post_processing_stuff_here();
  }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top