Views Bulk Operations - Creating custom action
by Ionut AlexucViews Bulk OperationsVBO (Views Bulk Operations) reuses and extends Drupal core Action system. Most of the Drupal core actions can be used as a VBO-compatible action.
Define the actions
First, we need to implement hook_action_info().
function MYMODULE_action_info() {
return array(
'MYMODULE_my_custom_action' => array(
'type' => 'node',
'label' => t('Search and replace text in a field'),
'configurable' => FALSE,
'vbo_configurable' => TRUE,
'triggers' => array('any'),
),
);
}
Add a VBO configuration form
We can add extra settings to the VBO configuration form in the Views UI.
function mymodule_my_custom_action_views_bulk_operations_form($options) {
$form = array();
$form['hero'] = array(
'#type' => 'select',
'#title' => t('Choose your super hero'),
'#options' => array(
'Iron Man' => t('Iron Man'),
'Bat Man' => t('Bat Man'),
),
'#default_value' => !empty($options['hero']) ? $options['hero'] : '',
);
return $form;
}
Add a per-bulk configuration form as well
If you need to make your action flexible, that there could be a global View-wide setting as well as a per-bulk setting, we can do that as well! Observe that the $settings variable contains the information from the Views configuration form, while $form_state is passed by reference and contains the normal form state information in case you need to modify the form state (for example, to attach field widgets) during the setup of the per-bulk form. This function should return the $form variable for the form you want the per-bulk settings page to use.
function mymodule_my_custom_action_form($settings, &$form_state) {
$form = array();
$form['hero'] = array(
'#type' => 'select',
'#title' => t('Choose your super hero'),
'#options' => array(
'Iron Man' => t('Iron Man'),
'Bat Man' => t('Bat Man'),
),
'#required' => TRUE,
'#default_value' => isset($settings['settings']['hero']) ? $settings['settings']['hero'] : '',
);
return $form;
}
function mymodule_my_custom_action_submit($form, $form_state) {
$return = array();
$return['hero'] = $form_state['values']['hero'];
return $return;
}
This form will be presented everytime we are about to execute some action on a set of chosen items.
ACTION!
It's time to create our action. So far, we have a View-wide setting form added, and there will be a form everytime to reselect the configuration for each bulk. The function name below comes from the hook_action_info() array that was built in the first steps of this article.
function mymodule_my_custom_action(&$node, $context) {
$message = t('Node title is %title. Sincerely, %hero', array(
'%title' => $node->title,
'%hero' => $context['hero'],
));
drupal_set_message($message);
}
$node is the node object. If you are executing the action on a user entity, this obviously will be the user object. $context variable will contain our per-bulk setting and the view-wide setting. This variable is also useful to determine the batch's position (current item, total number of items), and it contains the entity type. rows in the $context array will be empty because we do not have aggregation in this action (I will add an example soon).
That's it!
You defined your custom action, added a configuration form, added a modification form, and created the actual action function!
Read more: https://www.drupal.org/node/2052067
- Log in to post comments