taxonomy_save_term

5 taxonomy.module taxonomy_save_term(&$form_values)
6 taxonomy.module taxonomy_save_term(&$form_values)

Helper function for taxonomy_form_term_submit().

Parameters

$form_state['values']:

Return value

Status constant indicating if term was inserted or updated.

4 calls to taxonomy_save_term()

File

modules/taxonomy/taxonomy.module, line 306
Enables the organization of content into categories.

Code

function taxonomy_save_term(&$form_values) {
  $form_values += array(
    'description' => '', 
    'weight' => 0,
  );

  if (!empty($form_values['tid']) && $form_values['name']) {
    drupal_write_record('term_data', $form_values, 'tid');
    $hook = 'update';
    $status = SAVED_UPDATED;
  }
  else if (!empty($form_values['tid'])) {
    return taxonomy_del_term($form_values['tid']);
  }
  else {
    drupal_write_record('term_data', $form_values);
    $hook = 'insert';
    $status = SAVED_NEW;
  }

  db_query('DELETE FROM {term_relation} WHERE tid1 = %d OR tid2 = %d', $form_values['tid'], $form_values['tid']);
  if (!empty($form_values['relations'])) {
    foreach ($form_values['relations'] as $related_id) {
      if ($related_id != 0) {
        db_query('INSERT INTO {term_relation} (tid1, tid2) VALUES (%d, %d)', $form_values['tid'], $related_id);
      }
    }
  }

  db_query('DELETE FROM {term_hierarchy} WHERE tid = %d', $form_values['tid']);
  if (!isset($form_values['parent']) || empty($form_values['parent'])) {
    $form_values['parent'] = array(0);
  }
  if (is_array($form_values['parent'])) {
    foreach ($form_values['parent'] as $parent) {
      if (is_array($parent)) {
        foreach ($parent as $tid) {
          db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $form_values['tid'], $tid);
        }
      }
      else {
        db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $form_values['tid'], $parent);
      }
    }
  }
  else {
    db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $form_values['tid'], $form_values['parent']);
  }

  db_query('DELETE FROM {term_synonym} WHERE tid = %d', $form_values['tid']);
  if (!empty($form_values['synonyms'])) {
    foreach (explode("\n", str_replace("\r", '', $form_values['synonyms'])) as $synonym) {
      if ($synonym) {
        db_query("INSERT INTO {term_synonym} (tid, name) VALUES (%d, '%s')", $form_values['tid'], chop($synonym));
      }
    }
  }

  if (isset($hook)) {
    module_invoke_all('taxonomy', $hook, 'term', $form_values);
  }

  cache_clear_all();

  return $status;
}

Comments

Programmatically Inserting a term

<?php
  $term
= array(
   
'vid' => 5, // Voacabulary ID
   
'name' => 'Drupal', // Term Name
   
'synonyms' => 'Druplet', // (Optional) Synonym of this term
   
'parent' => 11, // (Optional) Term ID of a parent term 
   
'relations' => array(15), // (Optional) Related Term IDs
 
);

 
taxonomy_save_term($term);

 
// If there are more than one synonyms then you can concatenate each synonym with '\r\n' , i.e.
  // 'synonyms' => "Druplet\r\nDrup\r\nUltimate Drupal", // These are three synonyms - Druplet, Drup, Ultimate Drupal

  // If there are more than one parent then you can add each parent in an array i.e.
  // 'parent' => array(11, 10, 13),
  // You can nest as many parents as you want
  // 'parent' => array(11, array(9, 10), 13),

  // If there are more than one relations to this term then you can add each relation in an array i.e.
  // 'relations' => array(15, 16, 17),
?>

Correction

The $term array should be :

$term = array(
'vid' => 5, // Voacabulary ID
'name' => 'Drupal', // Term Name
'synonyms' => 'Druplet', // (Optional) Synonym of this term
'parent' => array(11), // (Optional) Term ID of a parent term
'relations' => array(15), // (Optional) Related Term IDs
);

Note that 'parent' must be an array.

Retrieving the tid

It may be useful to know that after calling taxonomy_save_term($term), the $term array gets updated with data from the newly created term. Hence, the tid for the new term can easily be found in $term['tid'].

The new tid is always an

The new tid is always an array not an object

How to remove

What's the equivalent of this

What's the equivalent of this function for drupal 7?

taxonomy_term_save

taxonomy_term_save

What is the top snippet for?

This snippet from the top of the function seems strange - some sort of comment to explain it would be a good idea. I might post this as a bug - docs clearly state that comments should be used to explain code which is not easily understood.

$form_values += array(
'description' => '',
'weight' => 0,
);

working perfect

hi,
You can use this one. Following is shortest way to create a term.

$term = array(
'vid' => 1, // Voacabulary ID
'name' => $newterm, // Term Name
);
$qr = taxonomy_save_term($term);

Return new TID instead of status?

I don't fully understand how the code creates a new Taxonomy Term. I'm a little curious as to the reasoning of just returning a 1 for a successfully created term instead of the new TID.

I am currently involved in a project using Services. We are adding new taxonomy terms via Services. Services is just dumping what it gets from this function, so it makes the information only somewhat useful instead of awesome!

Login or register to post comments