0

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 thread 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"

Can somebody give me some guidelines, or even an explanation how to achieve this?

  • 1
    Do you want to copy the field settings or the field value? when do you want to copy the value? – David Thomas Feb 23 '13 at 8:25
  • Good question David, now your asking that, I have to admit I used the wrong description. I would like to copy the field value, not the settings. This has to be done once for almost all my content items (nodes). I think I can use VBO for that but not sure how to achieve this. – Herbiek Feb 23 '13 at 10:28
  • David, did you have perhaps also have an explanation how to archieve the copy action I described? – Herbiek Mar 4 '13 at 8:37
0

You could do something like this:

$result = db_query("SELECT nid FROM {node} WHERE type = 'my_type'");
foreach($result as $row){
  $node = node_load($row->nid, NULL, TRUE); // reset cache
  // Assign one node reference field to another
  $node->field_termreference_2 = $node->field_termreference_1;
  node_save($node);
}

If you've got a lot of nodes, you may need to use the Batch API for that.

Alternatively, you may be able to copy the field tables to another field table at the database level with SQL ( backup first )

Something like:

INSERT INTO field_data_field_termreference_2 SELECT * FROM field_data_field_termreference_1;
INSERT INTO field_revision_field_termreference_2 SELECT * FROM field_revision_field_termreference_1;

and clear Drupal caches after that.

  • Thanks David..Not sure where to put the code you've given me. I think I give it a try to use execute the SQL code. – Herbiek Mar 4 '13 at 19:26
0

I ran into the same issue last week. If you want you can try the sandbox project I created with the working title "field_value_copy"

It uses Entity API to load, fetch values and set them on the target field.

Put the module in you modules folder, run drush cache-clear drush and the you execute the command with

drush fieldcopy field_termreference_1 field_termreference_2 my_content_type

If you want to try it on just one node you can run

drush fieldcopy field_termreference_1 field_termreference_2 my_content_type node 12

where 12 is the node id.

0

Adding to what David Thomas said, about using INSERT INTO SELECT statement, http://dev.mysql.com/doc/refman/5.1/en/insert-select.html - reference

INSERT INTO field_data_field_[term reference 2] SELECT * FROM field_data_field_[term reference 1];

I was able to use the above insert statement to copy term/table to another term reference table.

MySQL approach is a simple solution and works for my use case because how often will I need to merge or copy from one term reference to another? If I'm writing a module it would make sense. If I'm doing this just once, MySQL is simpler solution.

  • Sam

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.