You are here

taxonomy_machine_name.install in Taxonomy Machine Name 7

Same filename and directory in other branches
  1. 8 taxonomy_machine_name.install

Taxonomy Machine Name Install File.

File

taxonomy_machine_name.install
View source
<?php

/**
 * @file
 * Taxonomy Machine Name Install File.
 */

/**
 * Implements hook_install().
 */
function taxonomy_machine_name_install() {
  $settings = array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => FALSE,
    'default' => NULL,
    'description' => 'The taxonomy term machine name.',
  );
  db_add_field('taxonomy_term_data', 'machine_name', $settings);
  db_add_unique_key('taxonomy_term_data', 'vid_machine_name', array(
    'vid',
    'machine_name',
  ));
  db_add_index('taxonomy_term_data', 'machine_name', array(
    'machine_name',
  ));

  // Disable default migrate taxonomy handler.
  taxonomy_machine_name_update_7001();
}

/**
 * Implements hook_uninstall().
 */
function taxonomy_machine_name_uninstall() {
  db_drop_unique_key('taxonomy_term_data', 'vid_machine_name');
  db_drop_index('taxonomy_term_data', 'machine_name');
  db_drop_field('taxonomy_term_data', 'machine_name');

  // Restore default migrate taxonomy handler.
  $disabled = unserialize(variable_get('migrate_disabled_handlers', serialize(array())));
  $position = array_search('MigrateTaxonomyTermReferenceFieldHandler', $disabled);
  if (!is_null($position)) {
    $disabled = array_values($disabled);
    variable_set('migrate_disabled_handlers', serialize($disabled));
  }
}

/**
 * Implements hook_schema_alter().
 */
function taxonomy_machine_name_schema_alter(&$schema) {
  $schema['taxonomy_term_data']['fields']['machine_name'] = array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => FALSE,
    'default' => NULL,
    'description' => 'The taxonomy term machine name.',
  );
  $schema['taxonomy_term_data']['unique keys']['vid_machine_name'] = array(
    'vid',
    'machine_name',
  );
  $schema['taxonomy_term_data']['indexes']['machine_name'] = array(
    'machine_name',
  );
}

/**
 * Implements hook_enable().
 */
function taxonomy_machine_name_enable() {
  $vocabularies = taxonomy_vocabulary_get_names();
  foreach ($vocabularies as $vocabulary) {
    $terms = taxonomy_term_load_multiple(array(), array(
      'vid' => $vocabulary->vid,
    ));
    foreach ($terms as $term) {
      if (empty($term->machine_name)) {
        taxonomy_term_save($term);
      }
    }
  }
}

/**
 * Disable native migrate taxonomy field handler.
 */
function taxonomy_machine_name_update_7001() {
  $disabled = unserialize(variable_get('migrate_disabled_handlers', serialize(array())));
  $disabled[] = 'MigrateTaxonomyTermReferenceFieldHandler';
  $disabled = array_unique($disabled);
  variable_set('migrate_disabled_handlers', serialize($disabled));
}