You are here

function geocoder_field_field_formatter_settings_form in Geocoder 7.2

Implements hook_field_formatter_settings_form().

File

modules/geocoder_field/geocoder_field.module, line 590

Code

function geocoder_field_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $form = array();
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $geocoder_plugins = (array) $settings['geocoder_handlers'];
  $plugins = array_combine(array_values(\Drupal\geocoder\Geocoder::getPlugins('Provider')), array_values(\Drupal\geocoder\Geocoder::getPlugins('Provider')));
  $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(
              'fields',
              $field['field_name'],
              'settings_edit_form',
              '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(
              'fields',
              $field['field_name'],
              'settings_edit_form',
              '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['default_country'] = array(
    '#type' => 'select',
    '#title' => t('Default country'),
    '#options' => array(
      'site_default' => t('- Site default -'),
    ) + _addressfield_country_options_list(),
    '#default_value' => $settings['default_country'],
    '#empty_value' => '',
  );
  $form['format_handlers'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Format handlers'),
    '#options' => addressfield_format_plugins_options(),
    '#process' => array(
      'form_process_checkboxes',
      '_addressfield_field_formatter_settings_form_process_add_state',
    ),
    '#element_validate' => array(
      '_addressfield_field_formatter_settings_form_validate',
    ),
    '#default_value' => $settings['format_handlers'],
  );
  return $form;
}