| 7 taxonomy.module | taxonomy_term_save($term) |
| 8 taxonomy.module | taxonomy_term_save(TaxonomyTerm $term) |
Saves a term object to the database.
Parameters
$term: The taxonomy term object with the following properties:
- vid: The ID of the vocabulary the term is assigned to.
- name: The name of the term.
- tid: (optional) The unique ID for the term being saved. If $term->tid is empty or omitted, a new term will be inserted.
- description: (optional) The term's description.
- format: (optional) The text format for the term's description.
- weight: (optional) The weight of this term in relation to other terms within the same vocabulary.
- parent: (optional) The parent term(s) for this term. This can be a single term ID or an array of term IDs. A value of 0 means this term does not have any parents. When omitting this variable during an update, the existing hierarchy for the term remains unchanged.
- vocabulary_machine_name: (optional) The machine name of the vocabulary the term is assigned to. If not given, this value will be set automatically by loading the vocabulary based on $term->vid.
- original: (optional) The original taxonomy term object before any changes were applied. When omitted, the unchanged taxonomy term object is loaded from the database and stored in this property.
Since a taxonomy term is an entity, any fields contained in the term object are saved alongside the term object.
Return value
Status constant indicating whether term was inserted (SAVED_NEW) or updated (SAVED_UPDATED). When inserting a new term, $term->tid will contain the term ID of the newly created term.
14 calls to taxonomy_term_save()
File
- modules/
taxonomy/ taxonomy.module, line 614 - Enables the organization of content into categories.
Code
function taxonomy_term_save($term) {
// 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;
}
// Load the stored entity, if any.
if (!empty($term->tid) && !isset($term->original)) {
$term->original = entity_load_unchanged('taxonomy_term', $term->tid);
}
field_attach_presave('taxonomy_term', $term);
module_invoke_all('taxonomy_term_presave', $term);
module_invoke_all('entity_presave', $term, 'taxonomy_term');
if (empty($term->tid)) {
$op = 'insert';
$status = drupal_write_record('taxonomy_term_data', $term);
field_attach_insert('taxonomy_term', $term);
if (!isset($term->parent)) {
$term->parent = array(0);
}
}
else {
$op = 'update';
$status = drupal_write_record('taxonomy_term_data', $term, 'tid');
field_attach_update('taxonomy_term', $term);
if (isset($term->parent)) {
db_delete('taxonomy_term_hierarchy')
->condition('tid', $term->tid)
->execute();
}
}
if (isset($term->parent)) {
if (!is_array($term->parent)) {
$term->parent = array($term->parent);
}
$query = db_insert('taxonomy_term_hierarchy')
->fields(array('tid', '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();
}
// Reset the taxonomy term static variables.
taxonomy_terms_static_reset();
// Invoke the taxonomy hooks.
module_invoke_all("taxonomy_term_$op", $term);
module_invoke_all("entity_$op", $term, 'taxonomy_term');
unset($term->original);
return $status;
}
Login or register to post comments
Comments
What's the point of vocabulary_machine_name
I don't understand what's the point of vocabulary_machine_name param in term object. It would be useful if we could specify
vidORvocabulary_machine_nameto define the destination vocabulary, but asvidis required param, I don't see any sence invocabulary_machine_nameparam.Want to use machine_name
Bump.
I'd like to specify only the vocabulary_machine_name, as this is readily available in the code I have but vid is not.
Can vid be set to 0 or NULL or an equally meaningless value, and preference be given to vocabulary_machine_name?
You are right the vid is
You are right the vid is carfully hidden in the admin gui completly. Not one path has teh id instead of the name in it. I had to go into the database for finding the vid for the method to use.
Function for simpler term creation
A quick/simple function you can use to programmatically create taxonomy terms more easily (for example, while importing a bunch of content) and get the tid as a result (just pass in a term name and the vocabulary ID):
<?php/**
* Create a taxonomy term and return the tid.
*/
function custom_create_taxonomy_term($name, $vid) {
$term = new stdClass();
$term->name = $name;
$term->vid = $vid;
taxonomy_term_save($term);
return $term->tid;
}
?>
The taxonomy_term_save not return the tid when created
Hello,
I have a problem, when i use the function taxonomy_term_save($term) for create a new term of the taxonomy, the function doesn't return the $term->tid...
The function should not be : function taxonomy_term_save(&$term) { ... } with the params passed in rerefence ?
My version of Drupal is 7.12 ?
Thank you for your return / confirmation.
Sébastien
Objects are always passed by
Objects are always passed by reference, so if you want the tid, then it'll be in the term object that you pass to taxonomy_term_save(), e.g.
taxonomy_term_save($new_term);$tid = $new_term->tid;
Return new TID instead of status?
I understand that historically people have just referenced the $term after they sent it through the function, but doesn't this seem a little arcane?
I really appreciate the solution, but there are so many modules blindly using this function (such as Services) where it doesn't make sense to pass back a 1 or 0 or 2, but instead the TID of the new item. This way, just like when I create a new file and receive back the FID, I can also create a new taxonomy term and receive its new TID.
Range of weight values
The term weights are integers. The range changes based on how many terms are in the vocabulary and can be positive or negative.
Example: I have 5 terms in my vocabulary, so valid weights are [-5,5] or -5,-4,-3,-2,-1,0,1,2,3,4.5.