Copying data between fields with VBO

Last updated on
30 April 2025

In the Drupal forums an often requested feature is how to copy, move or merge data from Drupal core or CCK fields. Examples:

  • Copy the contents of the core body field into a CCK text field,
  • copy the contents of the core title field into a CCK field,
  • copy the contents of a CCK text field into another text field.

Operations like this required either coding in PHP or direct database manipulations in SQL. In many of these cases, the Views Bulk Operations (VBO) module provides a significant amount of help.

This handbook page gives a couple of examples how to use the incredible power of VBO. Please feel free to add more elaborate examples, if you can.

Prerequisites

Required contributed modules

To use Views Bulk Operations (VBO), you'll need a recent version of Views. To create custom fields, you'll need the Content Construction Kit (CCK) or Drupal 7.

Most of the copying operations require the "Execute arbitrary PHP script" action provided by VBO.

Caveats

Important: As always when doing bulk operations, don't do this on a live site and have database backups ready in case that something goes wrong!

Also make sure to set the VBO execution setting to "Use Batch API" to avoid timeouts.

Copy contents of node title into a CCK field

You want to copy the contents of the core node title into a CCK field with the machine readable name field_title.

  1. Select the nodes you want to process with VBO as usual;
  2. Select the VBO action "Execute arbitrary PHP script";
  3. For Drupal 6, enter the following PHP snippet into the textarea:
    $object->field_title[0]['value'] = $object->title;
    node_save($object);
    
  4. For Drupal 7, substitute $entity for $object, and use the following format for field: field_title[$entity->language][0]['value']:
    $entity->field_title[$entity->language][0]['value'] = $entity->title;
    node_save($entity);

Make sure to replace field_title with the actual machine name of your field, which you can find on the CCK manage fields page.

Copy contents of a CCK field into the node body

You want to copy the contents of a CCK field with the machine readable name field_text into the core node body.

  1. Select the nodes you want to process with VBO as usual;
  2. Select the VBO action "Execute arbitrary PHP script";
  3. For Drupal 6, enter the following PHP snippet into the textarea:
    $object->body = $object->field_text[0]['value'];
    node_save($object);
    
  4. For Drupal 7, substitute $entity with $object. See example PHP snippet:
    $entity->body['und'][0]['value'] = $entity->field_text['und'][0]['value'];
    node_save($entity);
    

Make sure to replace field_text with the actual machine name of your field, which you can find on the CCK manage fields page.

Copy contents of the node body into a CCK field

You want to copy the contents of the core node body into a CCK field:

return array(
  0 => array('value' => $node->body, 'format' => $node->format), // assuming the CCK field accepts a format as well
);

(Unchecked code from #852020: How to copy contents of Body field to CCK field?)

Copy contents of a CCK node reference field to another CCK node reference field

  1. Select the nodes you want to process with VBO as usual;
  2. Select the VBO action "Execute arbitrary PHP script";
  3. For Drupal 7, use the following:
    $entity->field_location2[$entity->language][0]['nid'] = $entity->field_location[$entity->language][0]['nid'];
    node_save($entity);
    

Make sure to replace field_location2and field_location with the actual machine name of your fields, which you can find on the CCK manage fields page. In this example we are copying the node reference field location to location2. This means that for each node, the field location2 gets a copy of the value from field_location of that same node. Why? That's a whole different discussion.

Alternative approaches

For certain cases you might want o to consider an alternative approach, e.g. by using Rules and Features. Examples:

One more approach, using VBO user interface capabilities and tokens: Drupal 7: Copying fields inside nodes using VBO

Sources and Credits

Help improve this page

Page status: Not set

You can: