我有一個視圖顯示內容。我正在使用VBO(視圖批量操作)從視圖中選擇一組行,並執行一些批量操作。該操作正在執行一個規則組件,其中要執行的實際操作作爲規則操作提供。如何在執行規則B之前或之後觸發規則A?
但是,我想在執行上述Rule組件之前和之後執行一些PHP操作。有沒有辦法做到這一點?使用視圖?
我有一個視圖顯示內容。我正在使用VBO(視圖批量操作)從視圖中選擇一組行,並執行一些批量操作。該操作正在執行一個規則組件,其中要執行的實際操作作爲規則操作提供。如何在執行規則B之前或之後觸發規則A?
但是,我想在執行上述Rule組件之前和之後執行一些PHP操作。有沒有辦法做到這一點?使用視圖?
由於我沒有找到任何其他解決方案,我砍了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個操作。
根據你的問題,因爲你有一個VBO行動有一個變量:參數集。你應該爲添加2個新的變量的2個動作。一個作爲參數(之前)和一個作爲提供(之後),然後添加採取之前動作值的2個動作。
如果規則操作集不允許其他變量需要克隆它。
對於批量操作選擇的視圖的每一行,規則組件都會執行一次。如果我向同一個規則組件添加更多的動作,它們將被執行多次與目標動作相同的次數。 我需要的是在批量操作開始之前要執行的'Pre Action'和在批量操作完成後要執行的'Post Action'。現在,我已經破解了VBO模塊的代碼,並在'views_bulk_operations_execute()'和'views_bulk_operations_execute_finished()'中的'Post Action'中添加了'Pre Action'。 但是,我想要做到這一點,而不會影響VBO模塊。 – MadCoder
我用一個自定義模塊來使用hook_action_info()創建我自己的操作。您可以使用the guide on 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();
}
}
破解貢獻模塊並不是一個好習慣。相反,你必須在你自己的模塊上使用鉤子函數。 – TheodorosPloumis