Last updated 30 June 2014. Created on 16 December 2010.
Edited by selwynpolit, d.novikov, adam_b, jmcoder. Log in to edit this page.

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

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

catofcheshir’s picture

Adding content taxonomy field to replace existing standart taxonomy widgets, i need to keep old taxonomy values with new field.
Thanks to VBO, it's simple. Just use VBO's "modify node fields" operation with PHP code like this:

foreach ($node->taxonomy as $term) {
  if ($term->vid == 6) {
    $terms[] = array('value'=>$term->tid);
  }
}
if ($terms) {return $terms;}

It handle multiple taxonomy vocabulary. For your needs replace vocablulary vid with vid of your content taxonomy field ("6" in my case)

amirtaiar’s picture

Thank you for that tip.
I was trying:

 field_url_fueld[$entity->language][0]['value'] = $entity->title;
node_save($entity);

And it's not working well.

Further more - Is there a way this line will active automatically and prevent the editor of the site to do so?

Managing Partner - Reasonat Web & Digital development.

amirtaiar’s picture

OK - The line seppose to be like this for Drupal 7:

$entity->field_url_fueld[$entity->language][0]['value'] = $entity->title;
node_save($entity);

Still trying to find a way to auto create it.

Managing Partner - Reasonat Web & Digital development.

herbiek’s picture

Hi, I have two fields (fieldtype = term reference) in the same content type and I would like to copy the settings from the first to the second one. After searching for a solution I found this threat, but I think the description I need is just the one that is not available: "Copy contents of a CCK node reference field to another CCK node reference field"

I'm using Drupal 7. Can somebody give me some guidelines, or even an explanation how to achieve this?

magicmirror’s picture

I'm in the same boat. Would love some help on this one myself.

pontus_nilsson’s picture

If you want to try another way, I've setup a sandbox that uses Entity API to copy field data executed through Drush. http://drupal.org/sandbox/pontusnilsson/1932784

herbiek’s picture

First of all I have to get myself familiar with the use of Drush (which was on my task list anyway), but secondly is your experimental Drush command also useful for copying a node reference field to another node reference field?

pontus_nilsson’s picture

Yes. I'll be happy to answer more questions about the module in the issue queue for the sandbox.

magicmirror’s picture

I'm going to dive into this sandbox project later today. I'm excited, and thank you for your effort!

herbiek’s picture

@MagicMirror: See also the answers on my question at stackexchange

magicmirror’s picture

That's a great start on solving this VBO way of doing things. I'd been looking for a clue on that for quite some time now! Thank you. Now if I could only get the field naming conventions clear for various addressfield and location field pieces, I would be at the finish line.

Also trying out the drush command as well in the sandbox. Lot's of good potential here. Thanks for all the contributions!

lovedrupal6’s picture

$object->field_data1[0]['value'] = $object->field_data2[0]['value'] ;
node_save($object);

lovedrupal6’s picture

Correction

$object->field_data2[0]['value'] = $object->field_data1[0]['value'] ;
node_save($object);
fgjohnson’s picture

I found the explanation here fantastic!
http://smartwolverine.net/article/drupal-7-copying-fields-inside-nodes-u...

No code - Using existing tools.

All you need is a view with field1 containing the value to copy into field 2.
- Maybe all you need is the field to update... but having both acts as a simple visual check.

Granitize

Get It Done!
Asking the obvious questions.

Junro’s picture

$object->field_data2 = $object->field_data1 ;
node_save($object);
katrien_w’s picture

I have a content type A that has a node reference field to another content type B. I had to switch content type B to taxonomy terms.
I added a term reference field to content type A. Now I would like to batch copy the value of the node reference field to the term reference field. I know the nid and tid don't correspond but the node title and term title do. Can anybody help me with a snippet to get this done using vbo?
Thanks in advance!

MrPaulDriver’s picture

It would be good to see an example of copying file field data from one file field to another.

For example you, want to the data for a file field with the machine readable name field_pdf into field_attachment.

I have attempted

$object->field_data2[0]['value'] = $object->field_data1[0]['value'] ;
node_save($object);

but this did not work for me.

---
Paul Driver
www.easable.uk
Ilkley, West Yorkshire, UK

leisurman’s picture

I would like to merge, append and prepend content from field_box2 into field_box1. But I don't want to lose the content inside field_box1. How can I alter the code below to do this? This code will replace the content in the first field with the content from the second field.

$entity->field_box1['und'][0]['value'] = $entity->field_box2['und'][0]['value'];
node_save($entity);
leisurman’s picture

I was able to merge the fields together by using VBO with Modify entity values and then adding both tokens into the field

[node:field_box1]
[node:field_box2]