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_location2
and 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
Comments
Copy taxonomy values to content taxonomy field
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:
It handle multiple taxonomy vocabulary. For your needs replace vocablulary vid with vid of your content taxonomy field ("6" in my case)
Thank you for that tip. I was
Thank you for that tip.
I was trying:
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 at Reasonat
OK - The line seppose to be
OK - The line seppose to be like this for Drupal 7:
Still trying to find a way to auto create it.
Managing Partner at Reasonat
copy term reference from one to other field
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?
I'm in the same boat. Would
I'm in the same boat. Would love some help on this one myself.
If you want to try another
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
First of all I have to get
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?
Yes. I'll be happy to answer
Yes. I'll be happy to answer more questions about the module in the issue queue for the sandbox.
I'm going to dive into this
I'm going to dive into this sandbox project later today. I'm excited, and thank you for your effort!
@MagicMirror: See also the
@MagicMirror: See also the answers on my question at stackexchange
thanks
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!
to copy data from cck field field_data1 to another field_data2
$object->field_data1[0]['value'] = $object->field_data2[0]['value'] ;
node_save($object);
to copy data from cck field field_data1 to another field_data2
Correction
Field to field inj the SAME content type
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.
D6 Node reference field -> Node reference field
copy node reference field to corresponding term reference field
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!
Copy file field data from one file field to another?
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
but this did not work for me.
---
Paul Driver
www.easable.uk
Ilkley, West Yorkshire, UK
How can you append not just copy
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.
I was able to merge the
I was able to merge the fields together by using VBO with Modify entity values and then adding both tokens into the field
Copy to a field collection
Here's my situation: I have two content types, Type 1 and Type 2. In my node of Type 1, I have one entity field referencing to node of Type 2. Now I created a Field collection containing the same entity reference field. I would like to copy datas from the first entity reference field to the one included into this field collection, is it possible? And what would be the code please?