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

redsd’s picture

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:

taxonomy_term_save((object) array(
'name' => $parent,
'vid' => $vid,
));

change it into:

$newterm = new stdClass();
$newterm->name = $parent;
$newterm->vid = $vid;
taxonomy_term_save(($newterm);

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?

phpepe’s picture

It's not taxonomy_term_save, is taxonomy_save_term ! ! !

llillf’s picture

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

leex’s picture

No it's taxonomy_term_save, this question is about D7.

hargobind’s picture

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:

  // Create a new term programmatically.
  $newterm = new stdClass();
  $newterm->name = 'My term'; // The name of the term
  $newterm->vid = $vid; // The ID of the parent vocabulary
  $newterm->parent = 0; // This tells taxonomy that this is a top-level term
  taxonomy_term_save($newterm);

You can read more about a term's properties in taxonomy_term_save()

drupalviking’s picture

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:

  $vocabulary = new stdClass();
  $vocabulary->name = 'My Vocabulary';
  $vocabulary->machine_name = "my_vocabulary";
  $vocabulary->description = t('My description.');
  $vocabulary->module = 'taxonomy';

  taxonomy_vocabulary_save($vocabulary);

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