Drupal 7 - Bulk Field Update on Demand

Drupal 7 - Bulk Field Update on Demand

Submitted by Vitaly Muzichuk on Tue, 07/29/2014 - 11:48
conveyor belt

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.

This gives you the ability to add your custom action to VBO list.

 
<?php
function MYMODULE_action_info() {
  return array(
    'MYMODULE_my_custom_action' => array(
      // The type of object this action acts upon.
      'type' => 'entity',
      // The human-readable name of the action, which should be passed through the t() function for translation.
      'label' => t('Approve event'),
      // If FALSE, then the action doesn't require any extra configuration.
      'configurable' => FALSE,
      // Set this to TRUE if you want to pass row information to action $context
      'pass rows' => TRUE,
    ),
  );
}
  • Next we want to add logic to our custom action.

In your custom module implement hook_my_custom_action(&$node, $context).

<?php
function MYMODULE_my_custom_action(&$entity, $context = array()) { 
    // Make necessary changes.
	$entity->field_status['und'][0]['value'] = 'approved';
    // Save changes.
	node_save($entity);
	// Set a custom message after processing.
	drupal_set_message(t($entity->title . ' [' . $entity->nid . '] has been approved for publishing'), 'status');
}

Although this is a very simple custom operation, creating a custom bulk operation can be as complex as needed. For more detailed information, visit the Views Bulk Operations development guide (Drupal 7).