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.
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 