You are here

public function GeocodeOrigin::buildOptionsForm in Geocoder 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/GeofieldProximitySource/GeocodeOrigin.php \Drupal\geocoder\Plugin\GeofieldProximitySource\GeocodeOrigin::buildOptionsForm()

Builds the specific form elements for the geofield proximity plugin.

Parameters

array $form: The form element to build.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

array $options_parents: The values parents.

bool $is_exposed: The check/differentiate if it is part of an exposed form.

Overrides GeofieldProximitySourceBase::buildOptionsForm

File

src/Plugin/GeofieldProximitySource/GeocodeOrigin.php, line 189

Class

GeocodeOrigin
Defines 'Geocode Origin (with Autocomplete option)' proximity source plugin.

Namespace

Drupal\geocoder\Plugin\GeofieldProximitySource

Code

public function buildOptionsForm(array &$form, FormStateInterface $form_state, array $options_parents, $is_exposed = FALSE) {
  $form['origin_address'] = [
    '#title' => t('Origin'),
    '#type' => 'textfield',
    '#description' => t('Address, City, Zip-Code, Country, ...'),
    '#default_value' => $this->originAddress,
    '#attributes' => [
      'class' => [
        'address-input',
      ],
    ],
  ];
  if (!$is_exposed) {
    $form['origin_address']['#title'] = t('Default Origin');
    $form['origin_address']['#description'] = t('Address, City, Zip-Code, Country that would be set as Default Geocoded Address in the Exposed Filter');

    // Attach Geofield Map Library.
    $form['#attached']['library'] = [
      'geocoder/general',
    ];
    $plugins_settings = isset($this->configuration['plugins']) ? $this->configuration['plugins'] : [];

    // Get the enabled/selected plugins.
    $enabled_plugins = [];
    foreach ($plugins_settings as $plugin_id => $plugin) {
      if (!empty($plugin['checked'])) {
        $enabled_plugins[] = $plugin_id;
      }
    }

    // Generates the Draggable Table of Selectable Geocoder Plugins.
    $form['plugins'] = $this->providerPluginManager
      ->providersPluginsTableList($enabled_plugins);

    // Filter out the Geocoder Plugins that are not compatible with Geofield
    // Proximity Geocoding.
    $form['plugins'] = array_filter($form['plugins'], function ($e) {
      return !in_array($e, $this->incompatiblePlugins);
    }, ARRAY_FILTER_USE_KEY);

    // Set a validation for the plugins selection.
    $form['plugins']['#element_validate'] = [
      [
        get_class($this),
        'validatePluginsSettingsForm',
      ],
    ];
    $form['use_autocomplete'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t("Enable Autocomplete"),
      '#default_value' => $this->useAutocomplete,
      '#description' => $this
        ->t('Check this to activate the Autocomplete Geocoding in the Address Origin Input.</br>Note: This will increase/double the Quota of Geocoding operations requested to the selected Geocoder Providers<br>(requests related to the Autocomplete phase plus the ones related to the Exposed Filter Submission)'),
      '#states' => [
        'invisible' => [
          ':input[name="options[expose_button][checkbox][checkbox]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['settings'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Geocoder fine Settings'),
      '#open' => FALSE,
      '#states' => [
        'invisible' => [
          [
            ':input[name="options[source_configuration][use_autocomplete]"]' => [
              'checked' => FALSE,
            ],
          ],
          [
            ':input[name="options[expose_button][checkbox][checkbox]"]' => [
              'checked' => FALSE,
            ],
          ],
        ],
      ],
    ];
    $form['settings']['autocomplete'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Autocomplete Settings'),
      '#open' => TRUE,
    ];
    $form['settings']['autocomplete']['min_terms'] = [
      '#type' => 'number',
      '#default_value' => $this->minTerms,
      '#title' => $this
        ->t('The (minimum) number of terms for the Geocoder to start processing.'),
      '#description' => $this
        ->t('Valid values ​​for the widget are between 2 and 10. A too low value (<= 3) will affect the application Geocode Quota usage.<br>Try to increase this value if you are experiencing Quota usage matters.'),
      '#min' => 2,
      '#max' => 10,
      '#size' => 3,
    ];
    $form['settings']['autocomplete']['delay'] = [
      '#type' => 'number',
      '#default_value' => $this->delay,
      '#title' => $this
        ->t('The delay (in milliseconds) between pressing a key in the Address Input field and starting the Geocoder search.'),
      '#description' => $this
        ->t('Valid values ​​for the widget are multiples of 100, between 300 and 3000. A too low value (<= 300) will affect / increase the application Geocode Quota usage.<br>Try to increase this value if you are experiencing Quota usage matters.'),
      '#min' => 300,
      '#max' => 3000,
      '#step' => 100,
      '#size' => 4,
    ];
    $form['settings']['autocomplete']['address_format'] = [
      '#title' => t('Address Format'),
      '#type' => 'select',
      '#options' => $this->formatterPluginManager
        ->getPluginsAsOptions(),
      '#description' => t('The address formatter plugin, used for autocomplete suggestions'),
      '#default_value' => $this->addressFormat,
      '#attributes' => [
        'class' => [
          'address-format',
        ],
      ],
    ];
  }
  elseif ($this->useAutocomplete) {
    $form['#attributes']['class'][] = 'origin-address-autocomplete';
    $form['#attached']['library'] = [
      'geocoder/geocoder',
    ];
    $form['#attached']['drupalSettings'] = [
      'geocode_origin_autocomplete' => [
        'providers' => array_keys($this
          ->getEnabledProviderPlugins()),
        'minTerms' => $this->minTerms,
        'delay' => $this->delay,
        'address_format' => $this->addressFormat,
      ],
    ];
  }
}