Programatically add taxonomy vocabularies

In this example a tax_creator module is used to install vocabularies when activating the module and to delete the vocabularies when uninstalling. This is really timesaving when dealing with many vocabularies as the only thing needed to do is to build a vocabulary array and activate the module. Comment hook_uninstall when uninstalling so taxonomy categories are kept.

Building the .info file


name = Taxonomy vocabulary creator
description = Create taxonomy vocabularies by using the install hooks.
core = 7.x
version = 7.x-1.dev

Building the .install file


<?php
/**
 * @file: Install and uninstall hooks and functions.
 */

/**
 * Implements hook_install().
 */
function tax_creator_install() {
  
  // Build an array of taxonomy vocs to save.
  $vocabularies = _tax_creator_build_voc_array();

  // And process the array.
  foreach ($vocabularies as $vocabulary) {
    _tax_creator_create_tax_voc($vocabulary);    
  }
  
}

/**
 * Implements hook_uninstall().
 */
function tax_creator_uninstall() {

  // Build an array of taxonomy vocs to save.
  $vocabularies = _tax_creator_build_voc_array();
  
  // And process the array.
  foreach ($vocabularies as $vocabulary) {    
    _tax_creator_delete_tax_voc($vocabulary);
  }
  
}

/**
 *  Build an array containing arrays of vocabulary 
 *  'machine_name' and 'name' key value pairs.
 *  
 * @return array
 */
function _tax_creator_build_voc_array() {
  return array(
    array(
      'machine_name' => 'test_voc_1',
      'name' => 'Test vocabulary 1',
    ),
    array(
      'machine_name' => 'test_voc_2',
      'name' => 'Test vocabulary 2',
    ),
    array(
      'machine_name' => 'test_voc_3',
      'name' => 'Test vocabulary 3',
    ),
  );
}

/**
 * Create a taxonomy vocabulary.
 * @param array $vocabulary
 *  An array containing arrays of vocabulary 
 *  'machine_name' and 'name' key value pairs.
 */
function _tax_creator_create_tax_voc($vocabulary) {
  
  $vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE machine_name = :name", array(':name' => $vocabulary['machine_name']))->fetchField();
  
  if (!$vid) {
    // Save the vocabulary if it doesn't exist.
    taxonomy_vocabulary_save((object) array(
      'name' => $vocabulary['name'],
      'machine_name' => $vocabulary['machine_name'],
    ));
  
    // Save the vocabulary Id for later.
    // As taxonomy term save returns a save status, we need another query to collect the vid.
    variable_set(
      $vocabulary['machine_name'] . '_vid',
      db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE machine_name = :name", array(':name' => $vocabulary['machine_name']))->fetchField()
    );
    
    if(module_exists('dblog')) 
      watchdog('taxonomy', 'Taxonomy @name created', array('@name' => $vocabulary['name']));
  }
  
}

/**
 * Delete a taxonomy vocabulary and its related variable.
 * @param array $vocabulary
 *  An array containing arrays of vocabulary 
 *  'machine_name' and 'name' key value pairs.
 */
function _tax_creator_delete_tax_voc($vocabulary) {

  $variable = $vocabulary['machine_name'] . '_vid';
  $vid = variable_get($variable, null);
  
  if($vid) {
    taxonomy_vocabulary_delete($vid);
    variable_del($variable);
    
    if(module_exists('dblog'))
      watchdog('taxonomy', 'Taxonomy @name deleted', array('@name' => $vocabulary['name']));
  }
  
}

Building the .module file

Create a tax_creator.module file but leave it empty as we don't need any drupal hooks coming from the .module file for this operation.

In Drupal 7, each module needs to have a .module file, which is deprecated in Drupal 8.