You are here

public static function FieldChangeHelper::mergeFields in Helper 7

File

lib/FieldChangeHelper.php, line 336

Class

FieldChangeHelper

Code

public static function mergeFields($old_field_name, $new_field_name) {
  $old_field = field_info_field($old_field_name);
  $new_field = field_info_field($new_field_name);
  if (empty($old_field)) {
    throw new FieldException("Field {$old_field_name} does not exist.");
  }
  if (empty($new_field)) {
    throw new FieldException("Field {$new_field_name} does not exist.");
  }
  if ($old_field['type'] != $new_field['type']) {
    throw new FieldException("Cannot merge fields because they are not the same field type.");
  }
  if ($old_field['storage']['type'] !== 'field_sql_storage') {
    throw new FieldException("Unable to change field type for field {$old_field['field_name']} using storage {$old_field['storage']['type']}.");
  }
  if ($new_field['storage']['type'] !== 'field_sql_storage') {
    throw new FieldException("Unable to change field type for field {$new_field['field_name']} using storage {$new_field['storage']['type']}.");
  }
  $instances = field_read_instances(array(
    'field_name' => $old_field_name,
  ));
  foreach ($instances as $instance) {
    static::changeInstanceField($instance, $new_field_name);
  }

  // I don't think this is necessary since this will remove all the instances
  // for a field, which deletes the field as well.
  // FieldHelper::deleteField($old_field);
}