taxonomy_term_save

Versions
7
taxonomy_term_save($term)

Save a term object to the database.

Parameters

$term A term object.

Return value

Status constant indicating if term was inserted or updated.

▾ 3 functions call taxonomy_term_save()

forum_form_submit in modules/forum/forum.admin.inc
Process forum form and container form submissions.
taxonomy_autocomplete_validate in modules/taxonomy/taxonomy.module
Form element validate handler for taxonomy term autocomplete element.
taxonomy_form_term_submit in modules/taxonomy/taxonomy.admin.inc
Submit handler to insert or update a term.

Code

modules/taxonomy/taxonomy.module, line 461

<?php
function taxonomy_term_save($term) {
  if ($term->name) {
    // Prevent leading and trailing spaces in term names.
    $term->name = trim($term->name);
  }
  if (!isset($term->vocabulary_machine_name)) {
    $vocabulary = taxonomy_vocabulary_load($term->vid);
    $term->vocabulary_machine_name = $vocabulary->machine_name;
  }

  field_attach_presave('taxonomy_term', $term);

  if (!empty($term->tid) && $term->name) {
    $status = drupal_write_record('taxonomy_term_data', $term, 'tid');
    field_attach_update('taxonomy_term', $term);
    module_invoke_all('taxonomy_term_update', $term);
  }
  else {
    $status = drupal_write_record('taxonomy_term_data', $term);
    _taxonomy_clean_field_cache($term);
    field_attach_insert('taxonomy_term', $term);
    module_invoke_all('taxonomy_term_insert', $term);
  }

  db_delete('taxonomy_term_hierarchy')
    ->condition('tid', $term->tid)
    ->execute();

  if (!isset($term->parent) || empty($term->parent)) {
    $term->parent = array(0);
  }
  if (!is_array($term->parent)) {
    $term->parent = array($term->parent);
  }
  $query = db_insert('taxonomy_term_hierarchy')
    ->fields(array('tid', 'parent'));
  if (is_array($term->parent)) {
    foreach ($term->parent as $parent) {
      if (is_array($parent)) {
        foreach ($parent as $tid) {
          $query->values(array(
            'tid' => $term->tid,
            'parent' => $tid
          ));
        }
      }
      else {
        $query->values(array(
          'tid' => $term->tid,
          'parent' => $parent
        ));
      }
    }
  }
  $query->execute();

  cache_clear_all();
  taxonomy_terms_static_reset();

  return $status;
}
?>
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.