You are here

function geocoder_field_field_widget_settings_form in Geocoder 7.2

Implements hook_field_widget_settings_form().

File

modules/geocoder_field/geocoder_field.module, line 22

Code

function geocoder_field_field_widget_settings_form($this_field, $instance) {
  $valid_fields = array();
  $settings = $instance['widget']['settings'];
  $entity_fields = field_info_instances($instance['entity_type'], $instance['bundle']);
  $all_fields = field_info_fields();
  $entities = entity_get_info();

  // Add the title field to the list of fields on this entity
  $entity_keys = $entities[$instance['entity_type']]['entity keys'];

  // Some entities do not have a "title" field, such as Field Collection or
  // Message entities. If the entity does not have a label key, it will be
  // undefined or FALSE.
  if (isset($entity_keys['label']) && $entity_keys['label'] !== FALSE) {
    $title_field = $entities[$instance['entity_type']]['entity keys']['label'];
    $all_fields[$title_field] = array(
      'field_name' => $title_field,
      'type' => 'text',
    );
    $entity_fields['title']['label'] = t(drupal_ucfirst($title_field));
  }

  // 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)) {
      $valid_fields[$field['field_name']] = sprintf('%s (%s)', $entity_fields[$field['field_name']]['label'], $field['field_name']);
    }
  }

  // Extend with virtual fields.
  $info = entity_get_all_property_info($instance['entity_type']);
  foreach ($info as $property_name => $property) {
    if (isset($property['type']) && in_array($property['type'], array(
      'location',
      'text',
    ))) {
      if (!isset($valid_fields[$property_name])) {
        $valid_fields[$property_name] = sprintf('%s (%s)', $property['label'], $property_name);
      }
    }
  }
  natcasesort($valid_fields);
  switch ($instance['widget']['type']) {
    case 'geocoder':
      $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,
      );
      break;
  }
  $geocoder_plugins = (array) $settings['geocoder_handlers'];
  $providers = \Drupal\geocoder\Geocoder::getPlugins('Provider');
  $plugins = array_combine(array_values($providers), array_values($providers));
  $plugins_array = array();
  $i = 0;
  foreach ($plugins as $plugin) {
    $plugins_array[$plugin] = array(
      'name' => $plugin,
      'weight' => $i++,
      'enabled' => 0,
    );
  }
  $i = 0;
  foreach ($geocoder_plugins as $name => $option) {
    if (is_array($option)) {
      if ($option['enabled'] == 1) {
        $plugins_array[$name]['enabled'] = 1;
        $plugins_array[$name]['weight'] = isset($option['weight']) ? $option['weight'] : $i++;
      }
    }
    else {
      if ($plugins[$option]) {
        $plugins_array[$option]['enabled'] = 1;
        $plugins_array[$option]['weight'] = $i++;
      }
    }
  }
  uasort($plugins_array, function ($a, $b) {
    if ($a['enabled'] > $b['enabled']) {
      return -1;
    }
    elseif ($a['enabled'] < $b['enabled']) {
      return 1;
    }
    if ($a['weight'] < $b['weight']) {
      return -1;
    }
    elseif ($a['weight'] > $b['weight']) {
      return 1;
    }
    if ($a['name'] < $b['name']) {
      return -1;
    }
    elseif ($a['name'] > $b['name']) {
      return 1;
    }
    return 0;
  });
  $data = array();
  foreach ($plugins_array as $plugin) {
    $data[$plugin['name']] = array(
      'name' => $plugin['name'],
      'machine_name' => $plugin['name'],
      'weight' => $plugin['weight'],
      'enabled' => $plugin['enabled'],
    );
  }
  $rows = array();
  $row_elements = array();
  foreach ($data as $id => $entry) {
    $rows[$id] = array(
      'data' => array(
        array(
          'class',
          array(
            'entry-cross',
          ),
        ),
        array(
          'data' => array(
            '#type' => 'weight',
            '#title' => t('Weight'),
            '#title_display' => 'invisible',
            '#default_value' => $entry['weight'],
            '#parents' => array(
              'instance',
              'widget',
              'settings',
              'geocoder_handlers',
              $id,
              'weight',
            ),
            '#attributes' => array(
              'class' => array(
                'entry-order-weight',
              ),
            ),
          ),
        ),
        array(
          'data' => array(
            '#type' => 'checkbox',
            '#title' => t('Enable'),
            '#title_display' => 'invisible',
            '#default_value' => (bool) $entry['enabled'],
            '#parents' => array(
              'instance',
              'widget',
              'settings',
              'geocoder_handlers',
              $id,
              'enabled',
            ),
          ),
        ),
        check_plain($entry['name']),
      ),
      'class' => array(
        'draggable',
      ),
    );

    // Build rows of the form elements in the table.
    $row_elements[$id] = array(
      'weight' => &$rows[$id]['data'][1]['data'],
      'enabled' => &$rows[$id]['data'][2]['data'],
    );
  }
  $form['#tree'] = TRUE;

  // Add the table to the form.
  $form['geocoder_handlers'] = array(
    '#theme' => 'table',
    '#caption' => t('Select the geocoder plugin in use, from the top to the bottom.'),
    // The row form elements need to be processed and build,
    // therefore pass them as element children.
    'elements' => $row_elements,
    '#header' => array(
      // We need two empty columns for the weigth field and the cross.
      array(
        'data' => NULL,
        'colspan' => 2,
      ),
      t('Enabled'),
      t('Name'),
    ),
    '#rows' => $rows,
    '#empty' => t('There are no entries available.'),
    '#attributes' => array(
      'id' => 'entry-order-geocoder-handlers',
    ),
  );
  drupal_add_tabledrag('entry-order-geocoder-handlers', 'order', 'sibling', 'entry-order-weight');
  $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,
  );
  return $form;
}