1

I have a content-type with a certain taxonomy term field that allows multiple values.

I don't need this field to be a taxonomy term field anymore, but just a simple text field. So, I want to "transform" the nature of the field.

I do know it is not possible and that I have to create a new text field and copy the values of my first field into my second through a custom php script.

But I hardly think that I'm going to need some help from you guys for the SQL query that will be a bit complex.

  • I need to recover all the term's names for each piece of content
  • and, if there are several of them, concatenate them with a comma (for example),
  • and then copy the result in the new text field before deleting the old taxonomy field.

The truth is I have no idea how to proceed. Can I run the query directly in PhpMyAdmin? Or in a PHP file? Do I need to use a simple SQL query or an EntityFieldQuery class? I'm not very familiar with this kind of stuff.

Thanks in advance for any clue...

2
  • Do something opposite to this? :)
    – Mołot
    Sep 9 '14 at 8:28
  • Yes that exactly what I meant :D I found indeed a lot of stuff about text to taxonomy but nothing about the opposite.
    – Djouuuuh
    Sep 9 '14 at 8:47
1

According to me you can achieve it by doing something like:

Creating a PHP script which: Get all terms related to a node and then create a comma separated value for them and then store this as value of your new field in database.

You need to iterate over all nodes and the run following code to get all terms

$result = db_query(
SELECT ttd.tid, ttd.vid, ttd.name, ttd.description, ttd.weight
FROM taxonomy_term_data AS ttd
INNER JOIN taxonomy_index ti ON ti.tid = ttd.tid
WHERE ti.nid= :nid
ORDER BY ttd.weight, ttd.name',array(':nid' =>$node->nid));

Or you can use Drupal code as well for doing the same

$node = node_load(arg(1));
if ($node != NULL) {
$field = $node->field_my_vocabulary;
$lang= $node->language;
$terms_all = $field[$lang];
foreach ($terms_all as $term) {
$terms[] = $term['tid'];
}
return implode('+', $terms);
}

Now assign this vale to you new field.

The above code is not tested by me, but I hope it can do what you want with no/little modification :)

4
  • Thanks for your answer. I'll have a look at it tomorrow and I'll let you know :)
    – Djouuuuh
    Sep 9 '14 at 14:53
  • Indeed, with a little modifications, I can achieve that operation very easily. However, I use node_save() after assigning the new value of my field but it doesn't save anything. Am I misunderstanding the node_save() function? Can't it be used whenever we want?
    – Djouuuuh
    Sep 10 '14 at 9:52
  • 2
    You can always use node_save() to save naode manually but in proper way try reading this post it shows hoe exactly you can do proper node_save manually drupal.stackexchange.com/questions/8039/… Sep 12 '14 at 5:43
  • 1
    Here is the code that works fine (I tested with hook_node_view): function grap_log_node_view($node, $view_mode, $langcode) { if ($node != NULL && $node->type == 'contact') { $field = $node->field_title; $terms_all = $field['und']; foreach ($terms_all as $term) { $terms[] = $term['taxonomy_term']->name; } $result = implode(', ', $terms); $node->field_function_title['und'][0]['value'] = $result; $node = node_submit($node); node_save($node); } }
    – Djouuuuh
    Sep 12 '14 at 8:02

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.