i'm trying to create a hierarchical vocabulary of terms. In my scenario, I wanted to have a list of parent terms and corresponding children terms. and i get this error :
Fatal error: Call to undefined function taxonomy_term_save() in /home/user1/tuto/drupal/drupal-7.14/sites/default/modules/taxonomy/taxonomy.module on line 34
<?php
// $Id$
// Create the vocabulary.
function vocabulary_enable() {
$edit = array(
'name' => 'Event Type',
'machine_name' => 'event_type',
'description' => t('Use keywords to identify contents.'),
'module' => 'taxonomy',
);
$vocabulary = (object) $edit;
taxonomy_vocabulary_save($vocabulary);
}
// Get the vocabulary ID.
$vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE machine_name = 'event_type'")->fetchField();
// Define the terms.
$terms['Group 1'][] = 'Some term 1';
$terms['Group 1'][] = 'Some term 2';
$terms['Group 1'][] = 'Some term 3';
$terms['Group 2'][] = 'Another term 1';
$terms['Group 2'][] = 'Another term 2';
foreach ($terms as $parent => $children) {
// Create the parent term.
taxonomy_term_save((object) array(
'name' => $parent,
'vid' => $vid,
));
// Get the parent term's ID.
$tid = db_query("SELECT tid FROM {taxonomy_term_data} WHERE vid = :vid ORDER BY tid DESC LIMIT 1", array(
':vid' => $vid,
))->fetchField();
foreach ($children as $term) {
// Create the child term.
taxonomy_term_save((object) array(
'name' => $term,
'vid' => $vid,
'parent' => array($tid),
));
}
}
----------------
thinks in advance
Comments
When I look at you error I
When I look at you error I assume you are editing the taxonomy module, I won't advice it since you can't do any security updates anymore.
This will probally fix your problem:
change it into:
You also need to do this for the other taxonomy_term_save you used.
if this doesn't work, check if you didn't accidently changed the function name of taxonomy_term_save, or deleted it in total?
Its a function name problem
It's not
taxonomy_term_save, is taxonomy_save_term ! ! !Hi, I am new to Drupal. Can I
Hi, can I know how you make the code work? Do you create module and put the code in a .module file and enable the module?
Thanks a lot
No it's taxonomy_term_save,
No it's taxonomy_term_save, this question is about D7.
One more thing
There's one thing missing from the above code which is the "parent" property. Without it, the term doesn't show on the Vocabulary page. Here's the updated code:
You can read more about a term's properties in
taxonomy_term_save()Getting the vid of newly created Vocabulary
The function taxonomy_vocabulary_save() is a "magic" function, in the sense that when you run it, it actually changes the object you sent in (sorta like call by reference, even if it isn't), so when you have a code like this:
the taxonomy_vocabulary_save adds five fields to your object: vid, hierarchy, weight, language and i18n_mode. So you can very easily access your vid by using this code:
$vid = $vocabulary->vid;and skip the database search that is posted in this post. Let's do it "The Drupal Way" :-D