Community

Creating vocabularies and taxonomy terms programmatically :Fatal error: Call to undefined function taxonomy_term_save()

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:

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

change it into:

<?php
$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?

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

Go Drupal :)

nobody click here