Drupal 7 - Bulk Field Update on Demand

2014-07-29

Vitaly Muzichuk

Drupal 7 - Bulk Field Update on Demand

How to implement custom Views Bulk Operations actions to update fields on multiple nodes in Drupal 7.

While building an event organization website, there was a requirement to have each event approved by a legal department before the organizer was allowed to publish any event.

A custom status field with conditions based on this field was the best approach.

Updating / rewriting a field for multiple nodes on demand can be achieved with Views and Views Bulk Operations.

Enable Views and Views Bulk Operations. In your custom module implement hook_action_info():

function MYMODULE_action_info() {
  return array(
    'MYMODULE_my_custom_action' => array(
      'type' => 'entity',
      'label' => t('Approve event'),
      'configurable' => FALSE,
      'pass rows' => TRUE,
    ),
  );
}

Next we want to add logic to our custom action. In your custom module implement hook_my_custom_action(&$node, $context):

function MYMODULE_my_custom_action(&$entity, $context = array()) {
  $entity->field_status['und'][0]['value'] = 'approved';
  node_save($entity);
  drupal_set_message(t($entity->title . ' [' . $entity->nid . '] has been approved for publishing'), 'status');
}

For more detailed information, visit the Views Bulk Operations development guide (Drupal 7).