taxonomy_save_term

Versions
4.6
taxonomy_save_term($edit)
4.7
taxonomy_save_term(&$edit)
5 – 6
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 functions call taxonomy_save_term()

forum_form_submit in modules/forum/forum.admin.inc
Process forum form and container form submissions.
taxonomy_form_term_submit in modules/taxonomy/taxonomy.admin.inc
Submit handler to insert or update a term.
taxonomy_node_save in modules/taxonomy/taxonomy.module
Save term associations for a given node.
taxonomy_overview_terms_submit in modules/taxonomy/taxonomy.admin.inc
Submit handler for terms overview form.

Code

modules/taxonomy/taxonomy.module, line 302

<?php
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;
}
?>

In order to programmatically insert terms...

MrMac - Sun, 2009-10-18 21:56

You can use this function. just proceed as follows:

$vid = 5; //put here the vocabulary ID of your interest
$term = 'drupal"; //but here the term you want to insert
$edit = array('vid' => $vid, 'name' => $term);
taxonomy_save_term($edit);

Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.