You are here

content_taxonomy_migrate.module in Content Taxonomy 7

File

content_taxonomy_migrate.module
View source
<?php

/**
 * @file
 *
 */

/**
 * Implements hook_content_migrate_field_alter().
 */
function content_taxonomy_migrate_content_migrate_field_alter(&$field_value, $instance_value) {
  if ($field_value['type'] == 'content_taxonomy') {
    $field_value['type'] = 'taxonomy_term_reference';
    $field_value['module'] = 'taxonomy';

    // Transform field settings.
    $old_settings = $field_value['settings'];
    $vocabulary = taxonomy_vocabulary_load($old_settings['vid']);
    $field_value['settings'] = array();
    $field_value['settings']['allowed_values'] = array();
    $field_value['settings']['allowed_values'][0]['vocabulary'] = $vocabulary->machine_name;
    $field_value['settings']['allowed_values'][0]['parent'] = $old_settings['parent'];
  }
}

/**
 * Implements hook_content_migrate_instance_alter().
 */
function content_taxonomy_migrate_content_migrate_instance_alter(&$instance_value, $field_value) {

  // Only work on content_taxonomy fields.
  if ($field_value['type'] != 'content_taxonomy') {
    return;
  }

  // Track whether fields are managed by this module.
  $fix_this_instance = FALSE;

  // Fix the widget.
  if ($instance_value['widget_type'] == "content_taxonomy_autocomplete") {
    $fix_this_instance = TRUE;
    $instance_value['widget_type'] = 'taxonomy_autocomplete';
    $instance_value['widget']['type'] = 'taxonomy_autocomplete';
    $instance_value['widget']['module'] = 'taxonomy';
  }
  elseif ($instance_value['widget_type'] == "content_taxonomy_options" || $instance_value['widget_type'] == "content_taxonomy_tree") {
    $fix_this_instance = TRUE;
    $instance_value['widget_type'] = 'options_buttons';
    $instance_value['widget']['type'] = 'options_buttons';
    $instance_value['widget']['module'] = 'options';
  }
  elseif ($instance_value['widget_type'] == "content_taxonomy_select" || $instance_value['widget_type'] == 'content_taxonomy_hs') {
    $fix_this_instance = TRUE;
    $instance_value['widget_type'] = 'options_select';
    $instance_value['widget']['type'] = 'options_select';
    $instance_value['widget']['module'] = 'options';
  }

  // Fix the formatter.
  foreach ($instance_value['display'] as $context => $settings) {
    if ($instance_value['display'][$context]['type'] == 'default') {
      $fix_this_instance = TRUE;
      $instance_value['display'][$context]['type'] = 'taxonomy_term_reference_plain';
    }
    elseif ($instance_value['display'][$context]['type'] == 'link') {
      $fix_this_instance = TRUE;
      $instance_value['display'][$context]['type'] = 'taxonomy_term_reference_link';
    }
    if ($instance_value['display'][$context]['module'] == 'content_taxonomy_options') {
      $fix_this_instance = TRUE;
      $instance_value['display'][$context]['module'] = 'options';
    }
  }

  // Fix the defaults.
  if ($fix_this_instance && !empty($instance_value['default_value'])) {
    foreach ($instance_value['default_value'] as $key => $default) {

      // Need to check isset() and is_null() because the value could be NULL.
      if (isset($instance_value['default_value'][$key]['value']) || is_null($instance_value['default_value'][$key]['value'])) {

        // Remove any empty 'value' strings.
        if (empty($instance_value['default_value'][$key]['value'])) {
          unset($instance_value['default_value'][$key]['value']);
        }
        else {
          $instance_value['default_value'][$key]['tid'] = $instance_value['default_value'][$key]['value'];
          unset($instance_value['default_value'][$key]['value']);
        }

        // Remove a junk value carried over from D6.
        if (isset($instance_value['default_value'][$key]['_error_element'])) {
          unset($instance_value['default_value'][$key]['_error_element']);
          $updated = TRUE;
        }

        // If the array is empty, just remove it.
        if (empty($instance_value['default_value'][$key])) {
          unset($instance_value['default_value'][$key]);
        }
      }

      // There are no default values left.
      if (empty($instance_value['default_value'])) {
        $instance_value['default_value'] = NULL;
      }
    }
  }
}

/**
 * Implements hook_content_migrate_data_record_alter().
 */
function content_taxonomy_migrate_content_migrate_data_record_alter(&$record, $field) {
  if ($field['type'] == 'taxonomy_term_reference') {

    // Copy field_FIELD_NAME_value (D6) to field_FIELD_NAME_tid (D7).
    if (isset($record[$field['field_name'] . '_value']) && !isset($record[$field['field_name'] . '_tid'])) {
      $record[$field['field_name'] . '_tid'] = $record[$field['field_name'] . '_value'];
    }

    // Fill the taxonomy_index.
    if (variable_get('taxonomy_maintain_index_table', TRUE) && $field['storage']['type'] == 'field_sql_storage' && $record['entity_type'] == 'node') {
      if (isset($record[$field['field_name'] . "_tid"]) && $record[$field['field_name'] . "_tid"]) {
        $entity = node_load($record['entity_id']);
        db_delete('taxonomy_index')
          ->condition('nid', $entity->nid)
          ->condition('tid', $record[$field['field_name'] . "_tid"])
          ->execute();
        $query = db_insert('taxonomy_index')
          ->fields(array(
          'nid',
          'tid',
          'sticky',
          'created',
        ));
        $query
          ->values(array(
          'nid' => $entity->nid,
          'tid' => $record[$field['field_name'] . "_tid"],
          'sticky' => $entity->sticky,
          'created' => $entity->created,
        ));
        $query
          ->execute();
      }
    }
  }
}