1

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

但是,我想在执行上述规则组件之前和之后做一些 PHP 操作。有没有办法做到这一点?使用视图?

4

3 回答 3

0

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

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

如果规则操作集不允许附加变量,您必须克隆它。

于 2013-07-09T14:10:16.983 回答
0

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

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

如果您只想执行一次代码,请使用$current==2foreach 循环中的条件。如果您只想为一个特定视图执行代码,请将当前视图路径放入$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);

于 2013-07-17T21:07:13.667 回答
0

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

创建自定义操作的一部分涉及声明一个接受行和$context数组的回调函数,结构如下: 传递给动作回调的上下文数组的结构

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

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();
  }
}
于 2014-10-20T10:52:22.657 回答