I'd like to create a feature that creates a vocabulary and populates it with taxonomy terms.
Creating a vocabulary is easy with features and it can be done using UI (I only need to select the desired existing vocabulary and add it as dependency and a file features.taxonomy will be created)
However, there's no way to export terms to a feature, so I created a feature_name.install file and added the following function on it:
/**
* Implements hook_install().
*/
function feature_name_install() {
// Populates 'Vocabulary name' vocabulary with taxonomy terms.
$vocabulary = taxonomy_vocabulary_machine_name_load('vocabulary_machine_name');
$terms = array();
$terms[] = t('Term 1');
$terms[] = t('Term 2');
$terms[] = t('Term 3');
foreach($terms as $name) {
$term = new stdClass();
$term->vid = $vocabulary->vid;
$term->name = $name;
taxonomy_term_save($term);
}
}
The problem with this code is that when installing the feature it is loaded before creating the vocabulary so tries to add terms to a vocabulary that does not exist yet and produces an error.
I have tried using hook_post_features_enable_feature but I didn't succeeded (I may be using a wrong syntaxis, see code below).
/**
* Implements hook_post_features_enable_feature($component).
*/
function feature_name_post_features_enable_feature($component) {
$component = 'feature_name';
// Populates 'Vocabulary name' vocabulary with taxonomy terms.
$vocabulary = taxonomy_vocabulary_machine_name_load('vocabulary_machine_name');
$terms = array();
$terms[] = t('Term 1');
$terms[] = t('Term 2');
$terms[] = t('Term 3');
foreach($terms as $name) {
$term = new stdClass();
$term->vid = $vocabulary->vid;
$term->name = $name;
taxonomy_term_save($term);
}
}
Can anyone tell me how to solve this?