Saves a vocabulary.
$vocabulary: A vocabulary object with the following properties:
Status constant indicating whether the vocabulary was inserted (SAVED_NEW) or updated (SAVED_UPDATED).
function taxonomy_vocabulary_save($vocabulary) {
// Prevent leading and trailing spaces in vocabulary names.
if (!empty($vocabulary->name)) {
$vocabulary->name = trim($vocabulary->name);
}
// Load the stored entity, if any.
if (!empty($vocabulary->vid)) {
if (!isset($vocabulary->original)) {
$vocabulary->original = entity_load_unchanged('taxonomy_vocabulary', $vocabulary->vid);
}
// Make sure machine name changes are easily detected.
// @todo: Remove in Drupal 8, as it is deprecated by directly reading from
// $vocabulary->original.
$vocabulary->old_machine_name = $vocabulary->original->machine_name;
}
if (!isset($vocabulary->module)) {
$vocabulary->module = 'taxonomy';
}
module_invoke_all('taxonomy_vocabulary_presave', $vocabulary);
module_invoke_all('entity_presave', $vocabulary, 'taxonomy_vocabulary');
if (!empty($vocabulary->vid) && !empty($vocabulary->name)) {
$status = drupal_write_record('taxonomy_vocabulary', $vocabulary, 'vid');
taxonomy_vocabulary_static_reset(array(
$vocabulary->vid,
));
if ($vocabulary->old_machine_name != $vocabulary->machine_name) {
field_attach_rename_bundle('taxonomy_term', $vocabulary->old_machine_name, $vocabulary->machine_name);
}
module_invoke_all('taxonomy_vocabulary_update', $vocabulary);
module_invoke_all('entity_update', $vocabulary, 'taxonomy_vocabulary');
}
elseif (empty($vocabulary->vid)) {
$status = drupal_write_record('taxonomy_vocabulary', $vocabulary);
taxonomy_vocabulary_static_reset();
field_attach_create_bundle('taxonomy_term', $vocabulary->machine_name);
module_invoke_all('taxonomy_vocabulary_insert', $vocabulary);
module_invoke_all('entity_insert', $vocabulary, 'taxonomy_vocabulary');
}
unset($vocabulary->original);
cache_clear_all();
return $status;
}