3

Ok, I've installed features, uuid, uuid_features.

According to docs vocab terms shouls be exported too when creating taxonomy feature.

So I run a command:

drush fe my_feature taxonomy:tags

but every time inside feature there is no such thing like terms. Only vocabulary is exported. What I am doing wrong?

From code review I see that only hook_taxonomy_default_vocabularies is implemented.

1

2 Answers 2

5

You need to use the module uuid_features.

It is both possible to export individual terms by listing the ones you want or you can export all terms with the vocabulary:

  1. Enable uuid_features
  2. Go to admin/config/content/uuid_features
  3. Check the boxes of the Vocabularies under "Exportable Taxonomy term bundles"
    • If you want to export all terms then check the "Auto detect uuid terms" option, so future additions / changes will be added.
    • If you didn't select the "Auto detect uuid terms" option: Do a drush fc and you will see a new item "uuid_term", get the list of components and do a drush fe FEATURENAME uuid_term:THETERMSUUID1 uuid_term:THETERMSUUID2 (gets really long, hence the "Auto detect uuid terms" option)
    • If you selected the "Auto detect uuid terms" option: you just need to export the taxonomy so do a drush fe FEATURENAME taxonomy:VOCABNAME

Sorry about the odd bulletted list, trying to represent two options in one list.

0

The best way to export taxonomy terms without any extra modules is with update_hook_N() in your .install file.

Here's the code I use:

/**
 * Insert terms for MYVOCAB vocabulary.
 */
function MYMODULE_update_7101() {
  $terms = array(
    'Term #1',
    'Term #2',
    'Term #3',
    '...',
  );
  // Check if term exists in vocabulary and add it if not.
  MYMODULE_safe_add_terms($terms, 'MYVOCAB');
}

/**
 * Helper function for adding terms to existing vocabularies.
 *
 * @param array $term_names
 * @param string $vocabulary_machine_name
 * @param int $weight
 */
function MYMODULE_safe_add_terms($term_names = array(), $vocabulary_machine_name = '', $weight = 100) {
  //  Make sure the vocabulary exists.  This won't apply all desired options
  //  (description, etc.) but that's okay.  Features will do that later.
  //  For now, we just need somewhere to stuff the terms.
  $vocab = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name);

  // Load the vocabulary.
  // Check if field already exists and add it if not existing.
  if (is_object($vocab) && property_exists($vocab, 'vid') && $vocab->vid > 0) {
    //  Load each term.
    $i = 0;
    foreach ($term_names as $term_name) {
      $term = taxonomy_get_term_by_name($term_name, $vocabulary_machine_name);
      // Check if term exists and if it doesn't exist create a new one.
      if (count($term) == 0) {
        $term = (object)array(
          'name' => $term_name,
          'vid' => $vocab->vid,
          'weight' => $weight + $i,
        );
        // Save new term.
        taxonomy_term_save($term);
        $i++;
      }
    }
    drush_log($i . ' terms added to ' . $vocabulary_machine_name . ' vocabulary', 'notice');
  }
  else {
    drush_log('Vocabulary "' . $vocabulary_machine_name . '"" not found', 'error');
  }
}
3
  • That will only work if you already have the module enabled, since hook_update_N isn't called on install. hook_install doesn't work, because the vocabularies haven't been created by the feature yet.
    – akalata
    Apr 3, 2017 at 22:31
  • Of course. That's why you first need to have the vocabulary created (with features or manually). Having this hook_update_N() (even if you have it in the module where your features exports are - for the vocabulary) is good because you would be calling "drush features-revert-all" and after that "drush updb" to get the updates in.
    – alesr
    Apr 5, 2017 at 6:43
  • But the feature defining the vocabulary has to have been enabled before adding the hook_update with the terms. You can't enable a new feature module that will both define the vocabulary AND add the terms without creating the vocabulary as part of hook_install.
    – akalata
    Apr 6, 2017 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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