Taxonomy Terms in Code

In a recent project, I was tasked with creating a number of taxonomies, each having a number of predefined terms provided by the client. Because we were working on the initial build of the site, we wanted to keep these taxonomies in code so we could install them easily.

The Features module will handle the exporting of vocabularies with ease, but Drupal considers terms to be content, so I had to come up with a separate solution to install the terms.

In a recent project, I was tasked with creating a number of taxonomies, each having a number of predefined terms provided by the client. Because we were working on the initial build of the site, we wanted to keep these taxonomies in code so we could install them easily.

The Features module will handle the exporting of vocabularies with ease, but Drupal considers terms to be content, so I had to come up with a separate solution to install the terms.

In my research, I discovered a variety of approaches I could take to get terms into code; use Drupal’s hook_install(), the UUID_Features module or the Default Content module.

To keep things as simple as possible, and without relying on other modules, I went with the hook_install() method.

This following is an example of my install hook from my project_xyz_taxonomy_terms.install file.

  1. <span style="color: #000000;"><span style="color: #0000bb;"><?php
  2. </span><span style="color: #ff8000;">/**
  3. * Implementation of hook_install().
  4. */
  5. </span><span style="color: #007700;">function </span><span style="color: #0000bb;">project_xyz_taxonomy_terms_install</span><span style="color: #007700;">(){
  6. </span><span style="color: #ff8000;">//Compass taxonomy terms
  7. </span><span style="color: #0000bb;">$compass </span><span style="color: #007700;">= array(
  8. </span><span style="color: #dd0000;">'North'</span><span style="color: #007700;">,
  9. </span><span style="color: #dd0000;">'East'</span><span style="color: #007700;">,
  10. </span><span style="color: #dd0000;">'South'</span><span style="color: #007700;">,
  11. </span><span style="color: #dd0000;">'West'</span><span style="color: #007700;">,
  12. );
  13. </span><span style="color: #0000bb;">_project_xyz_taxonomy_terms_load_terms</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$compass</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'compass'</span><span style="color: #007700;">);
  14.  
  15. </span><span style="color: #ff8000;">//Seasons taxonomy terms
  16. </span><span style="color: #0000bb;">$seasons </span><span style="color: #007700;">= array(
  17. </span><span style="color: #dd0000;">'Winter'</span><span style="color: #007700;">,
  18. </span><span style="color: #dd0000;">'Spring'</span><span style="color: #007700;">,
  19. </span><span style="color: #dd0000;">'Summer'</span><span style="color: #007700;">,
  20. </span><span style="color: #dd0000;">'Fall'</span><span style="color: #007700;">,
  21. );
  22. </span><span style="color: #0000bb;">_project_xyz_taxonomy_terms_load_terms</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$seasons</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'seasons'</span><span style="color: #007700;">);
  23. }
  24. </span><span style="color: #0000bb;">?></span></span>

 

Also in the .install file, this function will perform the import of terms.

  1. <code><span style="color: #000000;"><span style="color: #0000bb;"><?php
  2. </span><span style="color: #ff8000;">/**
  3. * Custom function to load an array of terms into a specified vocabulary.
  4. */
  5. </span><span style="color: #007700;">function </span><span style="color: #0000bb;">_project_xyz_taxonomy_terms_load_terms</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$terms</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$vocab_name</span><span style="color: #007700;">){
  6. </span><span style="color: #0000bb;">$vocab </span><span style="color: #007700;">= </span><span style="color: #0000bb;">taxonomy_vocabulary_machine_name_load</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$vocab_name</span><span style="color: #007700;">);
  7. if (</span><span style="color: #0000bb;">$vocab </span><span style="color: #007700;">== </span><span style="color: #0000bb;">false</span><span style="color: #007700;">) {
  8. </span><span style="color: #0000bb;">drupal_set_message</span><span style="color: #007700;">(</span><span style="color: #dd0000;">'Error while attempting to install vocabulary ' </span><span style="color: #007700;">. </span><span style="color: #0000bb;">$vocab_name</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'error'</span><span style="color: #007700;">);
  9. }
  10. else {
  11. foreach(</span><span style="color: #0000bb;">$terms </span><span style="color: #007700;">as </span><span style="color: #0000bb;">$weight </span><span style="color: #007700;">=> </span><span style="color: #0000bb;">$term</span><span style="color: #007700;">){
  12. </span><span style="color: #0000bb;">$data </span><span style="color: #007700;">= new </span><span style="color: #0000bb;">stdClass</span><span style="color: #007700;">();
  13. </span><span style="color: #0000bb;">$data</span><span style="color: #007700;">-></span><span style="color: #0000bb;">name </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$term</span><span style="color: #007700;">;
  14. </span><span style="color: #0000bb;">$data</span><span style="color: #007700;">-></span><span style="color: #0000bb;">vid </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$vocab</span><span style="color: #007700;">-></span><span style="color: #0000bb;">vid</span><span style="color: #007700;">;
  15. </span><span style="color: #0000bb;">$data</span><span style="color: #007700;">-></span><span style="color: #0000bb;">weight </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$weight</span><span style="color: #007700;">;</span></span>

 

  1.       <span style="color: #0000bb;">taxonomy_term_save</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$data</span><span style="color: #007700;">);
  2. }
  3. }
  4. }
  5. </span><span style="color: #0000bb;">?></span>

 

Assumptions:
-We’ve exported a feature with these two vocabularies.
-The vocabularies feature is weighted higher than the ‘project_xyz_taxonomy_terms’ module (to ensure the vocabularies exist before trying to load terms into them).

Kevin King