You are here

function date_content_migrate_instance_alter in Date 7

Same name and namespace in other branches
  1. 7.3 date.field.inc \date_content_migrate_instance_alter()
  2. 7.2 date.field.inc \date_content_migrate_instance_alter()

Implements hook_content_migrate_instance_alter().

Use this to tweak the conversion of instance or widget settings from the D6 style to the D7 style for specific situations not handled by basic conversion, as when formatter or widget names or settings are changed.

File

./date.field.inc, line 441
Field hooks to implement a date field.

Code

function date_content_migrate_instance_alter(&$instance_value, $field_value) {
  switch ($instance_value['widget']['module']) {
    case 'date':

      // Some settings have been moved from field to instance.
      $default_format = $field_value['settings']['default_format'];
      unset($field_value['settings']['default_format']);
      $instance_value['settings']['repeat_collapsed'] = $field_value['settings']['repeat_collapsed'];
      unset($field_value['settings']['repeat_collapsed']);

      // Some settings have been moved from widget settings to instance settings.
      $instance_value['settings']['default_value'] = $instance_value['default_value'];
      unset($instance_value['default_value']);
      $instance_value['settings']['default_value_code'] = $instance_value['widget']['settings']['default_value_code'];
      unset($instance_value['widget']['settings']['default_value_code']);
      $instance_value['settings']['default_value2'] = $instance_value['widget']['settings']['default_value2'];
      unset($instance_value['widget']['settings']['default_value2']);
      $instance_value['settings']['default_value_code2'] = $instance_value['widget']['settings']['default_value_code2'];
      unset($instance_value['widget']['settings']['default_value_code2']);
      break;

      // We need to retrieve formatter settings from the variables and store them in the instance.
      // Some formatter names changed, and system date type names changed.
      $new_type = array(
        'format_interval' => 'format_interval',
        'default' => 'date_default',
        'small' => 'date_short',
        'large' => 'date_long',
        'medium' => 'date_medium',
      );
      $new_settings = array(
        'date_default' => array(
          'format_type' => $default_format,
          'show_repeat_rule' => 'show',
          'multiple_number' => '',
          'multiple_from' => '',
          'multiple_to' => '',
          'fromto' => 'both',
        ),
        'format_interval' => array(
          'interval' => 2,
        ),
        'date_short' => array(
          'format_type' => 'short',
          'show_repeat_rule' => 'show',
          'multiple_number' => '',
          'multiple_from' => '',
          'multiple_to' => '',
          'fromto' => 'both',
        ),
        'date_long' => array(
          'format_type' => 'long',
          'show_repeat_rule' => 'show',
          'multiple_number' => '',
          'multiple_from' => '',
          'multiple_to' => '',
          'fromto' => 'both',
        ),
        'date_medium' => array(
          'format_type' => 'medium',
          'show_repeat_rule' => 'show',
          'multiple_number' => '',
          'multiple_from' => '',
          'multiple_to' => '',
          'fromto' => 'both',
        ),
      );

      // Add the custom date format options to the list.
      $formats = array_keys(system_get_date_formats());
      foreach ($formats as $format) {
        if ($format != 'short' && $format != 'long' && $format != 'medium') {
          $new_type['date_' . $format] = $new_format;
          $new_settings['date_' . $format] = $new_settings['date_default'];
          $new_settings['date_' . $format]['format_type'] = $format;
        }
      }
      foreach ($instance_value['display'] as $context => $settings) {
        $instance_value['display'][$context]['type'] = $new_type[$settings['type']];
        $old_settings = date_old_formatter_get_settings($instance['field_name'], $instance['bundle'], $context);
        foreach ($new_settings as $key => $value) {
          $instance_value['display'][$context]['settings'][$key] = !empty($old_settings[$key]) ? $old_settings[$key] : $value;
        }
      }

      // We changed some of the field values, save them.
      field_info_update($field_values);
      break;
  }
}