You are here

function openlayers_field_widget_settings_form in Geofield 7.2

Implements hook_field_widget_settings_form().

File

./geofield.widgets.openlayers.inc, line 28
Provides widget hooks on behalf of Openlayers.

Code

function openlayers_field_widget_settings_form($field, $instance) {
  $widget = $instance['widget'];
  $settings = $widget['settings'];
  $form = array();

  // Get preset options, filtered to those which have the GeoField behavior and *don't* have the draw features behavior, which is incompatible
  $maps = openlayers_maps();
  $map_options = array();
  foreach ($maps as $map) {
    if (array_key_exists('openlayers_behavior_geofield', $map->data['behaviors']) && !array_key_exists('openlayers_behavior_drawfeatures', $map->data['behaviors'])) {
      $map_options[$map->name] = $map->title;
    }
  }
  if (empty($map_options)) {
    form_set_error('openlayers_map', "Error: You have no compatible openlayers maps. Make sure that at least one preset has the 'GeoField' behavior enabled and that it does not have the 'Draw Features' behavior enabled (which is incompatible).");
  }
  $form['openlayers_map'] = array(
    '#type' => 'select',
    '#title' => t('OpenLayers Map'),
    '#default_value' => isset($settings['openlayers_map']) ? $settings['openlayers_map'] : 'geofield_widget_map',
    '#options' => $map_options,
    '#description' => t('Select which OpenLayers map you would like to use. Only maps which have the GeoField behavior may be selected. If your preferred map is not here, add the GeoField behavior to it first. The "Draw Features" bahavior is incompatible - presets with this behavior are not shown.'),
  );
  $form['data_storage'] = array(
    '#type' => 'radios',
    '#title' => t('Storage Options'),
    '#description' => t('Should the widget only allow simple features (points, lines, or polygons), or should the widget allow for complex features? Note that changing this setting from complex to simple after data has been entered can lead to data loss.'),
    '#options' => array(
      'single' => 'Store each simple feature as a separate field.',
      'collection' => 'Store as a single collection.',
    ),
    '#default_value' => isset($settings['data_storage']) ? $settings['data_storage'] : 'single',
  );
  $form['feature_types'] = array(
    '#title' => t('Available Features'),
    '#type' => 'checkboxes',
    '#options' => array(
      'point' => t('Point'),
      'path' => t('Path'),
      'polygon' => t('Polygon'),
    ),
    '#description' => t('Select what features are available to draw.'),
    '#default_value' => isset($settings['feature_types']) ? $settings['feature_types'] : array(
      'point' => 'point',
      'path' => 'path',
      'polygon' => 'polygon',
    ),
  );
  $form['allow_edit'] = array(
    '#title' => t('Allow shape modification'),
    '#type' => 'checkbox',
    '#description' => t('Can you edit and delete shapes.'),
    '#default_value' => isset($settings['allow_edit']) ? $settings['allow_edit'] : 1,
  );

  // Add optional Geocoder support.
  $use_geocoder = isset($settings['use_geocoder']) ? $settings['use_geocoder'] : 0;
  $geocoder_form = array(
    '#type' => 'fieldset',
    '#title' => t('Geocoder settings'),
  );
  if (module_exists('geocoder')) {
    $geocoder_form['use_geocoder'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable geocoding of location data'),
      '#default_value' => $use_geocoder,
      // Can't nest this in a fieldset element without affecting data storage so instead hardcode one.
      '#prefix' => '<fieldset><legend><span="fieldset-legend">' . t('Geocoder settings') . '</span></legend><div class="fieldset-wrapper">',
    );

    // Load the Geocoder widget settings.
    module_load_include('inc', 'geocoder', 'geocoder.widget');
    $new = geocoder_field_widget_settings_form($field, $instance);

    // Show the geocoder fields only if geocoder is selected.
    openlayers_widget_add_states($new, ':input[name="instance[widget][settings][use_geocoder]"]');

    // Close the fieldset we opened in the #prefix to use_geocoder.
    $element_children = element_children($new);
    $new[end($element_children)]['#suffix'] = '</div></fieldset>';
    $geocoder_form += $new;
  }
  else {
    $geocoder_form['add_geocoder'] = array(
      '#markup' => t('Optionally, install the <a href="http//drupal.org/project/geocoder">Geocoder</a> module and add an <a href="http://drupal.org/project/addressfield">Address field</a> to enable mapping by address.'),
    );
  }
  $form += $geocoder_form;
  return $form;
}