5

As far as I know, the way to create a taxonomy term is having to go into the taxonomy module and creating on there, and THEN assigning the term to the node.

Is there a way to create taxonomy terms on the fly? When the node is created, if the taxonomy term doesn't exist, it should be created too.

4 Answers 4

7

Use the Hierarchical Select module:

Hierarchical Select has the ability to save the entire lineage of a selection or only the "deepest" selection. You can configure it to force the user to make a selection as deep as possible in the tree, or allow the user to select an item anywhere in the tree. Levels can be labeled, you can configure limit the number of items that can be selected, configure a title for the dropbox, choose a site-wide animation delay, and so on. You can even create new items and levels through Hierarchical Select!

Another way is selecting Autocomplete term widget (tagging) as field widget, when creating your term reference field

As another option, there is the Simple hierarchical select module.

1
  • 1
    I always thought Autocomplete was solely for searching already created terms. Thanks for telling me about it. Dec 29, 2011 at 16:41
2
 //see if this term already exists and fetch it if it does
  $term = taxonomy_get_term_by_name($i);

  //if it doesn't exist, make it
  if ($term == array()){
    //make a new class to hold the term for taxonomy 1
    $taxonomy = new stdClass();
    $taxonomy->name = $i;
    // mentioned vid here as of now i placed 1.
    $taxonomy->vid = 1;
    taxonomy_term_save($taxonomy);
    //now fetch it so we have it's tid
    $term = taxonomy_get_term_by_name($i);
  }

  //set $tid as it's returned from  taxonomy_get_term_by_name
  $tid = key($term);

  //tag the node with the appropriate tag
   $node->field_tags['und'][0] = array('tid' => $tid);

  //save the tagged node
  node_save($node);
  $i++;

Above code will check for term exist and if not creates new term, when creating new content.

2
  • This little snippet of code has help me oh so many times, that I just want to say thank you for sharing it.
    – Emil Orol
    Feb 2, 2016 at 21:30
  • Just to note here.. The the vid is not the version id as used for revisions, but its the Vocabulary ID. If you don't want to use the VID you can use $tazonomy->vocabulary_machine_name May 10, 2016 at 1:58
1

In addition to the above suggestions, once can go for autocomplete_delux(https://www.drupal.org/project/autocomplete_deluxe) and entity_autocreate(https://www.drupal.org/project/entityreference_autocreate) modules.

These behave in manner just as the above modules; plus they fall under Drupal Security Policy

-2

I'd like to suggest the Manage Multiple Terms module:

Manage Multiple Terms provides a simple mechanism to add, update, delete many terms in the same time easily and quickly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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