You are here

function openlayers_geofield_field_widget_settings_form in Openlayers 7.3

Implements hook_field_widget_settings_form().

Bases on openlayers_field_widget_settings_form()

See also

openlayers_field_widget_settings_form()

File

modules/openlayers_geofield/openlayers_geofield.module, line 264
Openlayers Geofield integration.

Code

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

  // Get preset options, filtered to those which have the GeoField component.
  $map_options = array();
  foreach (\Drupal\openlayers\Openlayers::loadAll('Map') as $map) {
    foreach ($map
      ->getObjects('component') as $component) {
      if ($component instanceof \Drupal\openlayers_geofield\Plugin\Component\GeofieldWidget\GeofieldWidget) {
        $map_options[$map
          ->getMachineName()] = $map
          ->getName();
      }
    }
  }
  if (empty($map_options)) {
    form_set_error('openlayers_map', 'Error: You have no compatible openlayers maps. Make sure that at least one map has the "GeoField Widget" component enabled.');
  }
  $form['openlayers_map'] = array(
    '#type' => 'select',
    '#title' => t('Openlayers Map'),
    '#default_value' => isset($settings['openlayers_map']) ? $settings['openlayers_map'] : 'openlayers_geofield_map_geofield',
    '#options' => $map_options,
    '#description' => t('Select which Openlayers map you would like to use. Only maps which have the "GeoField Widget" component may be selected. If your preferred map is not here, add the "GeoField Widget" component to it first. The "Draw Features" behavior 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,
  );
  $form['showInputField'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show input field'),
    '#description' => t('Shows the data in a textarea.'),
    '#default_value' => !empty($settings['showInputField']),
  );

  // 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);
    $new['geocoder_field']['#options'][$field['field_name']] = t('Add extra field');

    // If there are no options available search by ourselves to ensure the text
    // field type was taken in account.
    if (empty($new['geocoder_handler']['#options'])) {
      $supported_field_types = geocoder_supported_field_types();
      if (isset($supported_field_types['text'])) {
        $processors = geocoder_handler_info();
        foreach ($supported_field_types['text'] as $handler) {
          $new['geocoder_handler']['#options'][$handler] = $processors[$handler]['title'];
        }
      }
    }

    // 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;
}