You are here

function addressfield_update_7000 in Address Field 7

Update the field configuration to the new plugin structure.

File

./addressfield.install, line 117

Code

function addressfield_update_7000() {

  // Enable ctools.
  if (!module_enable(array(
    'ctools',
  ))) {
    throw new Exception('This version of addressfield requires ctools, but it could not be enabled.');
  }

  // Get the list of fields of type 'addressfield'.
  $address_fields = array();
  foreach (field_info_fields() as $field_name => $field_info) {
    if ($field_info['type'] == 'addressfield') {
      $address_fields[$field_name] = $field_name;
    }
  }
  foreach (field_info_instances() as $entity_type => $bundles) {
    foreach ($bundles as $bundle_name => $instances) {
      foreach (array_intersect_key($instances, $address_fields) as $field_name => $instance) {
        $widget_settings =& $instance['widget']['settings'];
        if ($instance['widget']['type'] == 'addressfield_standard') {

          // Default to use the country-based address widget.
          $format_handlers = array(
            'address',
          );

          // Map the old 'name_format' setting to the name and organization widgets.
          if (in_array($widget_settings['name_format'], array(
            'name_line_organisation',
            'first_last_organisation',
          ))) {
            $format_handlers[] = 'organisation';
          }
          if (in_array($widget_settings['name_format'], array(
            'name_line',
            'name_line_organisation',
          ))) {
            $format_handlers[] = 'name-oneline';
          }
          else {
            $format_handlers[] = 'name-full';
          }
          unset($widget_settings['name_format']);
          $widget_settings['format_handlers'] = $format_handlers;
        }

        // Update displays.
        foreach ($instance['display'] as $view_mode => &$view_mode_info) {
          $display_settings =& $view_mode_info['settings'];
          if ($view_mode_info['type'] == 'addressfield_default') {
            if (isset($widget_settings['format_handlers'])) {
              $display_settings['use_widget_handlers'] = 1;
            }
            else {

              // If the widget is non-standard, just use a sane default.
              $display_settings['use_widget_handlers'] = 0;
              $display_settings['format_handlers'] = array(
                'address',
                'name-oneline',
              );
            }
          }
          else {
            if ($view_mode_info['type'] == 'addressfield_name') {

              // Migrate the 'addressfield_name' formatter to the new framework.
              $view_mode_info['type'] = 'addressfield_default';

              // Start from the widget configuration.
              $display_settings['use_widget_handlers'] = 0;
              $display_settings['format_handlers'] = isset($widget_settings['format_handlers']) ? $widget_settings['format_handlers'] : array(
                'address',
                'name-oneline',
              );
              if (empty($display_settings['organisation'])) {
                $display_settings['format_handlers'] = array_diff($display_settings['format_handlers'], array(
                  'organisation',
                ));
              }
              unset($display_settings['organisation']);
            }
          }
        }
        field_update_instance($instance);
      }
    }
  }
}