On this page
- Prerequisites
- Required contributed modules
- Caveats
- Copy contents of node title into a CCK field
- Copy contents of a CCK field into the node body
- Copy contents of the node body into a CCK field
- Copy contents of a CCK node reference field to another CCK node reference field
- Alternative approaches
- Sources and Credits
Copying data between fields with VBO
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.
- Select the nodes you want to process with VBO as usual;
- Select the VBO action "Execute arbitrary PHP script";
- For Drupal 6, enter the following PHP snippet into the textarea:
$object->field_title[0]['value'] = $object->title; node_save($object); - 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.
- Select the nodes you want to process with VBO as usual;
- Select the VBO action "Execute arbitrary PHP script";
- For Drupal 6, enter the following PHP snippet into the textarea:
$object->body = $object->field_text[0]['value']; node_save($object); - 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
- Select the nodes you want to process with VBO as usual;
- Select the VBO action "Execute arbitrary PHP script";
- 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:
- Copy taxonomy terms from a node to a referenced node - copies any taxonomy terms from a story node to a referenced node
- Applying referenced node terms to parent - when a node that has references to other nodes is saved, the parent is given the merged terms of all of its children
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
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion