0

I'm new to PHP and Drupal module development. I'm having a site on which the users can create their own posts(nodes).

Can someone help me with how to save multiple taxonomy terms to a node. The users are filing their post in a form and the data is sent back through ajax. For example the taxonomy terms are sent to the server in this format: apple, banana, orange. I don't know how to save all these terms to a node.

1

First of all, be sure that the taxonomy reference field exist in manage field page of your content type, then you can attach that field to your node via the function field_attach_update, before attaching that field to node let's get it from your data passed through Ajax :

 $terms = $form_state['values']['your_tax_field'];

 // attach to node :
 $node = new stdClass();
 $node->nid = $nid // Make sure that the nid exist;
 $node->type = 'your_content_type';
 $node->your_tax_field = $terms;
 field_attach_update('node', $node);
 entity_get_controller('node')->resetCache(array($node->nid));

Not tested but should work like that! You can use devel module to debug your data passed in the form.

0

Go to Administration » Structure » Content types

Click on Manage Fields for Article.

Change the Widget Type of field Tags to Autocomplete term widget.

  • I need it to be through code in my custom module. – hakala May 30 '13 at 12:10
  • $node->taxonomy[] = $new_term; node_save($node); – Jithin May 30 '13 at 12:59
  • A made it to save the taxonomy term, but i can't get it to add the term to the node. Any suggestions what could be the problem? – hakala May 30 '13 at 14:48

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.