You are here

public static function FieldChangeHelper::changeInstances in Helper 7

1 call to FieldChangeHelper::changeInstances()
FieldChangeHelper::changeType in lib/FieldChangeHelper.php
Change a field's type, even if it has data.

File

lib/FieldChangeHelper.php, line 221

Class

FieldChangeHelper

Code

public static function changeInstances(array $field, array $field_instance_overrides = array()) {
  $type_info = field_info_field_types($field['type']);
  $instances = field_read_instances(array(
    'field_name' => $field['field_name'],
  ));
  foreach ($instances as $instance) {
    $prior_instance = $instance;

    // Serialize properties back into the data property so it can be saved
    // to the database.
    $instance['data'] = array();
    foreach ($instance as $key => $value) {
      switch ($key) {
        case 'id':
        case 'field_id':
        case 'field_name':
        case 'entity_type':
        case 'bundle':
        case 'deleted':
        case 'data':
          break;
        default:
          $instance['data'][$key] =& $instance[$key];
      }
    }
    $instance['settings'] = array_intersect_key($instance['settings'], $type_info['instance_settings']);
    $instance['settings'] += $type_info['instance_settings'];

    // Validate the existing widget can be used with the new field type.
    $widget_info = field_info_widget_types($instance['widget']['type']);
    if (!in_array($field['type'], $widget_info['field types'])) {

      // Fallback to using the field type's default widget.
      $instance['widget']['type'] = $type_info['default_widget'];
      $widget_info = field_info_widget_types($type_info['default_widget']);
      $instance['widget']['module'] = $widget_info['module'];
      $instance['widget']['settings'] = array_intersect_key($instance['widget']['settings'], $widget_info['settings']);
      $instance['widget']['settings'] += $widget_info['settings'];
    }

    // Validate the existing formatters can be used with the new field type.
    foreach ($instance['display'] as $view_mode => $display) {
      if ($display['type'] !== 'hidden') {
        $formatter_info = field_info_formatter_types($display['type']);
        if (!in_array($field['type'], $formatter_info['field types'])) {

          // Fallback to using the field type's default formatter.
          $instance['display'][$view_mode]['type'] = $type_info['default_formatter'];
          $formatter_info = field_info_formatter_types($type_info['default_formatter']);
          $instance['display'][$view_mode]['module'] = $formatter_info['module'];
          $instance['display'][$view_mode]['settings'] = array_intersect_key($instance['display'][$view_mode], $formatter_info['settings']);
          $instance['display'][$view_mode]['settings'] += $formatter_info['settings'];
        }
      }
    }

    // Allow anything to be overridden before it gets saved.
    $instance = drupal_array_merge_deep($instance, $field_instance_overrides);
    drupal_write_record('field_config_instance', $instance, array(
      'id',
    ));

    // Clear caches.
    field_cache_clear();
    module_invoke_all('field_update_instance', $instance, $prior_instance);
  }
}