Create custom action for Views Bulk Operations (with config form)

Public

In your custom module, define a action_info hook at first:

</> CopyGet raw version
php
  1. /**
  2.  * Implements hook_action_info().
  3.  */
  4. function custom_action_info() {
  5. return array(
  6. 'custom_action_callback_name' => array(
  7. 'type' => 'entity', // Can be file, term, user, etc.
  8. 'label' => t('Your label'),
  9. 'configurable' => TRUE, // Needs config form
  10. 'behavior' => array('view_property'), // Uses view access rights
  11. 'triggers' => array('any'), // Works always
  12. ),
  13. );
  14. }

Add configuration form callbacks. As an example, we want to get recipient emal address as a configuration parameter before action gets executed.

</> CopyGet raw version
php
  1. /**
  2.  * Return a form definition so the action can be configured.
  3.  */
  4. function custom_action_callback_name_form($context) {
  5. // Set default values for form.
  6. if (!isset($context['recipient'])) {
  7. $context['recipient'] = '';
  8. }
  9.  
  10. $form['recipient'] = array(
  11. '#type' => 'textfield',
  12. '#title' => t('Recipient'),
  13. '#default_value' => $context['recipient'],
  14. '#maxlength' => '254',
  15. '#description' => t('The email address to which the message should be sent OR enter [node:author:mail], [comment:author:mail], etc. if you would like to send an e-mail to the author of the original post.'),
  16. );
  17.  
  18. return $form;
  19. }
  20.  
  21. /**
  22.  * Validate custom_action_callback_name form submissions.
  23.  */
  24. function custom_vbo_action_send_node_validate($form, $form_state) {
  25. $form_values = $form_state['values'];
  26. // Validate the configuration form.
  27. if (!valid_email_address($form_values['recipient'])) {
  28. form_set_error('recipient', t('Enter a valid email address.'));
  29. }
  30. }
  31.  
  32. /**
  33.  * Process custom_action_callback_name form submissions.
  34.  */
  35. function custom_vbo_action_send_node_submit($form, $form_state) {
  36. $form_values = $form_state['values'];
  37. // Process the HTML form to store configuration. The keyed array that
  38. // we return will be serialized to the database.
  39. $params = array(
  40. 'recipient' => $form_values['recipient'],
  41. );
  42. return $params;
  43. }

Add action callback where you perform the action:

</> CopyGet raw version
php
  1. /**
  2.  * Add action callback where you perform the action.
  3.  */
  4. function custom_action_callback_name($entity, $context) {
  5. // Do the magic here.
  6. }

Comments

drubb's picture

If you need all selected entities at once in your action callback, just add 'aggregate' => TRUE in hook_action_info(). Your callback function will get an array of entity objects instead of a single one.

kiranking's picture

Can we get access to row no in action? I needed to set node field value to row no of views results. Is this possible?

kiranking's picture

how custom_vbo_action_send_node_validate function get called?