如何在执行规则B之前或之后触发规则A?

时间:2013-07-09 12:46:19

标签: drupal-7 drupal-views drupal-rules

我有一个显示内容的视图。我正在使用VBO(视图批量操作)从视图中选择一组行并执行一些批量操作。该操作正在执行Rule组件,其中要执行的实际操作被提供为规则操作。

但是,我想在执行上述Rule组件之前和之后执行一些PHP操作。有办法吗?使用视图?

3 个答案:

答案 0 :(得分:0)

只需在目标操作之前和之后插入要执行的2个操作。

根据您的问题,因为您有一个VBO动作,所以有一个变量:参数集。您应该为2个操作添加2个新变量。一个作为参数(之前),一个作为提供(之后),然后添加2个动作,从之前的动作获取值。

如果规则操作集不允许其他变量,则必须克隆它。

答案 1 :(得分:0)

由于我没有找到任何其他解决方案,我攻击了views_bulk_operations.module来完成我的工作。

views_bulk_operations_execute()函数中,在执行规则组件之前添加要执行的代码。在函数中提供的foreach循环中,添加自定义代码。

如果只想执行一次代码,那么在foreach循环中使用条件$current==2。如果您只想为一个特定视图执行代码,请将当前视图路径转换为$token变量,并将其与您在以下代码中给出的视图路径进行比较。

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];
}
}

views_bulk_operations_execute_finished()函数中,添加要在执行最后一行_views_bulk_operations_log($message);上方的规则组件后执行的代码。

答案 2 :(得分:0)

我使用自定义模块使用hook_action_info()创建自己的操作。您可以使用the guide on Drupal.org来帮助您创建自定义操作。

创建自定义操作的一部分涉及声明一个接受行和$context数组的回调函数,结构如下: Structure of context array passed into action callback

使用进度元素,您可以确定批量执行的距离。所以你的代码看起来像:

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();
  }
}