6

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?

1

The problem with exporting content like taxonomy terms is keeping the taxonomy id intact when migrating it from site to site which is exactly what the features extension module UUID Features solves by using the UUID api:

The UUID Features Integration module provides a mechanism for exporting content (nodes, taxonomy, fields) into a features module. What's that you say? You thought features was only for configuration? This module is meant to be used in the cases where certain pieces of content straddle the line between pure content and configuration.

1

Looks like your problem has to do with $component. The vocabulary isn't complete until $component == 'taxonomy' - you should do an if on this before trying to create terms.

1

Adding a vocabulary programmatically is just as easy as adding terms. You could use the same hook_install() function you've built for your term, and insert the vocabulary first.

function feature_name_install() {
  $vocab = new stdClass();
  $vocab->name = "My vocabulary name";
  $vocab->machine_name = "my_vocabulary_name";
  taxonomy_vocabulary_save($vocab);

  // Note, you could also attempt to load the vocabulary by machine name first,
  // which would allow you to take action if a vocabulary using your name
  // already exists (either delete it, or error out, or...)


  $vid = $vocab->vid;  // $vocab object now has the vocabulary ID

  // ...continue as you were in peace
0

Unfortunately the vocabulary provided by the feature won't be available using that hook, you'll have to use hook_post_features_rebuild() instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.