i want to create 1 taxonomy vocabulary and 1 taxonomy term using drupal's hook_enable. but when i enable the custom module, it create only the taxonomy vocabulary. And if i disable and enable the module, the taxonomy term will be created without problem. the code is as below:
$sql = "SELECT vid FROM {taxonomy_vocabulary} WHERE machine_name='article_type'";
$vid = db_query($sql)->fetchField();
if (!$vid) {
drupal_set_message(t("Created article type vocabulary"));
// No vocabulary exists, we'll create one.
$vocab = (object) array(
'name' => 'article type',
'description' => t('Automatically created by the custom_block module'),
'machine_name' => 'article_type',
'multiple' => '0',
'required' => '0',
'hierarchy' => '1',
'relations' => '0',
'module' => 'custom_block',
);
taxonomy_vocabulary_save($vocab);
$vid = $vocab->vid
}
$terms = (object) array (
'name' => 'News',
'vid' => $vid,
'vocabulary_machine_name' => 'article_type',);
$term_name = $terms -> name;
$tid = db_query("SELECT tid FROM {taxonomy_term_data} WHERE vid = :vid and name = :name", array(
':vid' => $vid,
':name' => $term_name,
))->fetchField();
if ($vid && !$tid) {
taxonomy_term_save($terms);
}
}
PS:i've figured it out! it's a stupid mistake. i did not get the vid after saving the vocabulary to the database. i just need to add $vid = $vocab->vid after taxonomy_term_save.