You are here

function commerce_file_update_7101 in Commerce File 7

Update fields for file scheme setting on field settings only

See also

http://drupal.org/node/1228854

File

./commerce_file.install, line 225
Install, update and uninstall functions this module.

Code

function commerce_file_update_7101(&$sandbox) {
  field_info_cache_clear();
  drupal_load('module', 'commerce_file');
  $field_type = COMMERCE_FILE_FIELD_TYPE;
  foreach (field_info_fields() as $field_name => $field) {
    if ($field['type'] != $field_type) {
      continue;
    }

    // field default scheme
    $field_scheme = isset($field['settings']['uri_scheme']) ? $field['settings']['uri_scheme'] : NULL;

    // clean up scheme options
    unset($field['settings']['uri_scheme_options'], $field['uri_scheme_options']);

    // update instance
    foreach ($field['bundles'] as $entity_type => $bundles) {
      foreach ($bundles as $bundle_name) {
        $instance = field_info_instance($entity_type, $field_name, $bundle_name);
        if (empty($instance)) {
          continue;
        }

        // extract file scheme and remove instance setting
        if (!isset($field_scheme)) {

          // use scheme from first found instance for field setting
          if (!empty($instance['settings']['uri_scheme'])) {
            $field_scheme = $instance['settings']['uri_scheme'];
          }
          elseif (!empty($instance['uri_scheme'])) {
            $field_scheme = $instance['uri_scheme'];
          }
        }
        unset($instance['settings']['uri_scheme'], $instance['uri_scheme'], $instance['widget']['settings']['uri_scheme']);

        // resolve file directory
        if (empty($instance['settings']['file_directory'])) {
          $directory = 'commerce-files';
          if (!empty($instance['file_directory'])) {
            $directory = $instance['file_directory'];
          }
          $instance['settings']['file_directory'] = $directory;
        }
        unset($instance['file_directory']);

        // remove extensions not in $instance['settings']
        unset($instance['file_extensions']);

        // save instance
        field_update_instance($instance);
      }
    }

    // update field scheme
    if (!isset($field_scheme)) {
      $field_scheme = _commerce_file_default_system_scheme();
      if (!empty($field['settings']['uri_scheme_options'])) {
        $field_scheme = reset($field['settings']['uri_scheme_options']);
      }
      elseif (!empty($field['uri_scheme_options'])) {
        $field_scheme = reset($field['uri_scheme_options']);
      }
    }

    // set default scheme
    $field['settings']['uri_scheme'] = $field_scheme;

    // save field
    field_update_field($field);
  }
  return t('Commerce File field settings have been updated.');
}