You are here

function geocoder_field_widget_settings_form in Geocoder 7

Implements field_widget_settings_form().

File

./geocoder.widget.inc, line 38
geocoder.widget.inc

Code

function geocoder_field_widget_settings_form($this_field, $instance) {
  $settings = $instance['widget']['settings'];
  $entity_fields = field_info_instances($instance['entity_type'], $instance['bundle']);
  $all_fields = field_info_fields();
  $supported_field_types = geocoder_supported_field_types();
  $processors = geocoder_handler_info();
  $handlers_by_type = array();
  $field_types = array();
  $valid_fields = array();
  $available_handlers = array();

  // Add in the title/name.
  // @@TODO Do this programatically by getting entity_info
  switch ($instance['entity_type']) {
    case 'node':
      $all_fields['title'] = array(
        'field_name' => 'title',
        'type' => 'text',
      );
      $entity_fields['title']['label'] = t('Title');
      break;
    case 'taxonomy_term':
      $all_fields['name'] = array(
        'field_name' => 'name',
        'type' => 'text',
      );
      $entity_fields['name']['label'] = t('Name');
      break;
    case 'country':
      $all_fields['name'] = array(
        'field_name' => 'name',
        'type' => 'text',
      );
      $entity_fields['name']['label'] = t('Name');
      break;
  }

  // Get a list of all valid fields that we both support and are part of this
  // entity.
  foreach ($all_fields as $field) {
    if (array_key_exists($field['field_name'], $entity_fields)) {
      if (in_array($field['type'], array_keys($supported_field_types), TRUE) && $field['field_name'] !== $this_field['field_name']) {
        $valid_fields[$field['field_name']] = $entity_fields[$field['field_name']]['label'];
        foreach ($supported_field_types[$field['type']] as $handler) {
          $available_handlers[$handler] = $processors[$handler]['title'];
          $handlers_by_type[$field['type']][] = $handler;
          $field_types[$field['field_name']] = $field['type'];
        }
      }
    }
  }

  // Extend with virtual fields.
  $info = entity_get_property_info($instance['entity_type']);
  foreach ($info as $property_name => $property) {
    if (isset($property['type']) && in_array($property['type'], array(
      'location',
      'text',
    ), TRUE)) {
      if (!isset($valid_fields[$property_name])) {
        $valid_fields[$property_name] = $property['label'];
      }
    }
  }
  natcasesort($valid_fields);
  $form['geocoder_field'] = array(
    '#type' => 'select',
    '#title' => t('Geocode from field'),
    '#default_value' => isset($settings['geocoder_field']) ? $settings['geocoder_field'] : '',
    '#options' => $valid_fields,
    '#description' => t('Select which field you would like to geocode from.'),
    '#required' => TRUE,
  );
  $form['geocoder_handler'] = array(
    '#type' => 'select',
    '#title' => t('Geocoder'),
    '#prefix' => '<div id="geocoder-handler-div">',
    '#suffix' => '</div>',
    '#default_value' => isset($settings['geocoder_handler']) ? $settings['geocoder_handler'] : '',
    '#options' => $available_handlers,
    '#description' => t('Select which type of geocoding handler you would like to use'),
    '#required' => TRUE,
  );
  $form['handler_settings'] = array(
    '#tree' => TRUE,
  );

  // Add the handler settings forms.
  foreach ($processors as $handler_id => $handler) {
    if (isset($handler['settings_callback']) || isset($handler['terms_of_service'])) {
      $default_values = isset($settings['handler_settings'][$handler_id]) ? $settings['handler_settings'][$handler_id] : array();
      $form['handler_settings'][$handler_id] = array();
      $form['handler_settings'][$handler_id]['#type'] = 'fieldset';
      $form['handler_settings'][$handler_id]['#attributes'] = array(
        'class' => array(
          'geocoder-handler-setting',
          'geocoder-handler-setting-' . $handler_id,
        ),
      );
      $form['handler_settings'][$handler_id]['#title'] = $handler['title'] . ' Settings';
      $form['handler_settings'][$handler_id]['#states'] = array(
        'visible' => array(
          ':input[id="edit-instance-widget-settings-geocoder-handler"]' => array(
            'value' => $handler_id,
          ),
        ),
      );
      if (isset($handler['terms_of_service'])) {
        $form['handler_settings'][$handler_id]['tos'] = array(
          '#type' => 'item',
          '#markup' => t('This handler has terms of service. Click the following link to learn more.') . ' ' . l($handler['terms_of_service'], $handler['terms_of_service']),
        );
      }
      if (isset($handler['settings_callback'])) {

        // Load the file.
        geocoder_get_handler($handler_id);
        $settings_callback = $handler['settings_callback'];
        $form['handler_settings'][$handler_id] = array_merge($form['handler_settings'][$handler_id], $settings_callback($default_values));
      }
    }
  }
  $form['delta_handling'] = array(
    '#type' => 'select',
    '#title' => t('Multi-value input handling'),
    '#description' => t('Should geometries from multiple inputs be: <ul><li>Matched with each input (e.g. One POINT for each address field)</li><li>Aggregated into a single MULTIPOINT geofield (e.g. One MULTIPOINT polygon from multiple address fields)</li><li>Broken up into multiple geometries (e.g. One MULTIPOINT to multiple POINTs.)</li></ul>'),
    '#default_value' => isset($settings['delta_handling']) ? $settings['delta_handling'] : 'default',
    '#options' => array(
      'default' => 'Match Multiples (default)',
      'm_to_s' => 'Multiple to Single',
      's_to_m' => 'Single to Multiple',
      'c_to_s' => 'Concatenate to Single',
      'c_to_m' => 'Concatenate to Multiple',
    ),
    '#required' => TRUE,
  );
  $form['latlng_override'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow manual lat/long override'),
    '#default_value' => isset($settings['latlng_override']) ? $settings['latlng_override'] : FALSE,
    '#description' => t('Allow the user to override geolocation lat/long values manually. Useful when geocoding is inaccurate.'),
  );

  // Add javascript to sync allowed values. Note that we are not using AJAX
  // because we do not have access to the raw form_state here.
  drupal_add_js(array(
    'geocoder_widget_settings' => array(
      'handlers' => $handlers_by_type,
      'types' => $field_types,
    ),
  ), 'setting');
  drupal_add_js(drupal_get_path('module', 'geocoder') . '/geocoder.admin.js', 'file');
  return $form;
}