You are here

function content_taxonomy_migrate_content_migrate_instance_alter in Content Taxonomy 7

Implements hook_content_migrate_instance_alter().

File

./content_taxonomy_migrate.module, line 28

Code

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;
      }
    }
  }
}