You are here

protected function LeafletSettingsElementsTrait::setGeocoderMapControl in Leaflet 2.1.x

Same name and namespace in other branches
  1. 8 src/LeafletSettingsElementsTrait.php \Drupal\leaflet\LeafletSettingsElementsTrait::setGeocoderMapControl()
  2. 2.0.x src/LeafletSettingsElementsTrait.php \Drupal\leaflet\LeafletSettingsElementsTrait::setGeocoderMapControl()

Set Map Geocoder Control Element.

Parameters

array $element: The Form element to alter.

array $settings: The Form Settings.

3 calls to LeafletSettingsElementsTrait::setGeocoderMapControl()
LeafletDefaultFormatter::settingsForm in src/Plugin/Field/FieldFormatter/LeafletDefaultFormatter.php
Returns a form to configure settings for the formatter.
LeafletDefaultWidget::settingsForm in src/Plugin/Field/FieldWidget/LeafletDefaultWidget.php
LeafletMap::buildOptionsForm in modules/leaflet_views/src/Plugin/views/style/LeafletMap.php
Provide a form to edit options for this plugin.

File

src/LeafletSettingsElementsTrait.php, line 882

Class

LeafletSettingsElementsTrait
Class LeafletSettingsElementsTrait.

Namespace

Drupal\leaflet

Code

protected function setGeocoderMapControl(array &$element, array $settings) {

  // Set Map Geocoder Control Element, if the Geocoder Module exists,
  // otherwise output a tip on Geocoder Module Integration.
  if ($this->moduleHandler
    ->moduleExists('geocoder') && class_exists('\\Drupal\\geocoder\\Controller\\GeocoderApiEnpoints')) {
    $default_settings = $this::getDefaultSettings();
    $element['geocoder'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Map Control - Geocoder'),
    ];
    $element['geocoder']['control'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable Map Geocoder Control'),
      '#description' => $this
        ->t('This will add a Geocoder control element to the Leaflet Map'),
      '#default_value' => isset($settings['geocoder']['control']) ? $settings['geocoder']['control'] : $default_settings['geocoder']['control'],
    ];
    $element['geocoder']['access_warning'] = [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#value' => $this
        ->t('<strong>Note: </strong>This will show to users with permissions to <u>Access Geocoder Api Url Enpoints.</u>'),
      '#attributes' => [
        'style' => 'color: red;',
      ],
    ];
    $element['geocoder']['settings'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Geocoder Settings'),
    ];
    $element['geocoder']['settings']['position'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Position'),
      '#options' => $this->controlPositionsOptions,
      '#default_value' => isset($settings['geocoder']['settings']['position']) ? $settings['geocoder']['settings']['position'] : $default_settings['geocoder']['settings']['position'],
    ];
    $element['geocoder']['settings']['input_size'] = [
      '#title' => $this
        ->t('Input Size'),
      '#type' => 'number',
      '#min' => 10,
      '#max' => 100,
      '#default_value' => isset($settings['geocoder']['settings']['input_size']) ? $settings['geocoder']['settings']['input_size'] : $default_settings['geocoder']['settings']['input_size'],
      '#description' => $this
        ->t('The characters size/length of the Geocoder Input element.'),
    ];
    $providers_settings = isset($settings['geocoder']['settings']['providers']) ? $settings['geocoder']['settings']['providers'] : [];

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

    // Generates the Draggable Table of Selectable Geocoder Providers.

    /** @var \Drupal\geocoder\ProviderPluginManager  $geocoder_provider */
    $geocoder_provider = \Drupal::service('plugin.manager.geocoder.provider');
    $element['geocoder']['settings']['providers'] = $geocoder_provider
      ->providersPluginsTableList($enabled_providers);

    // Set a validation for the providers selection.
    $element['geocoder']['settings']['providers']['#element_validate'] = [
      [
        get_class($this),
        'validateGeocoderProviders',
      ],
    ];
    $element['geocoder']['settings']['min_terms'] = [
      '#type' => 'number',
      '#default_value' => isset($settings['geocoder']['settings']['min_terms']) ? $settings['geocoder']['settings']['min_terms'] : $default_settings['geocoder']['settings']['min_terms'],
      '#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,
    ];
    $element['geocoder']['settings']['delay'] = [
      '#type' => 'number',
      '#default_value' => isset($settings['geocoder']['settings']['delay']) ? $settings['geocoder']['settings']['delay'] : $default_settings['geocoder']['settings']['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,
    ];
    $element['geocoder']['settings']['zoom'] = [
      '#title' => $this
        ->t('Zoom to Focus'),
      '#type' => 'number',
      '#min' => 1,
      '#max' => 22,
      '#default_value' => isset($settings['geocoder']['settings']['zoom']) ? $settings['geocoder']['settings']['zoom'] : $default_settings['geocoder']['settings']['zoom'],
      '#description' => $this
        ->t('Zoom level to Focus on the Map upon the Geocoder Address selection.'),
    ];
    $element['geocoder']['settings']['popup'] = [
      '#title' => $this
        ->t('Open Popup on Geocode Focus'),
      '#type' => 'checkbox',
      '#default_value' => isset($settings['geocoder']['settings']['popup']) ? $settings['geocoder']['settings']['popup'] : $default_settings['geocoder']['settings']['popup'],
      '#description' => $this
        ->t('Check this to open a Popup on the Map (with the found Address) upon the Geocode Focus.'),
    ];
    $element['geocoder']['settings']['options'] = [
      '#type' => 'textarea',
      '#rows' => 4,
      '#title' => $this
        ->t('Geocoder Control Specific Options'),
      '#description' => $this
        ->t('This settings would override general Geocoder Providers options. (<u>Note: This would work only for Geocoder 2.x branch/version.</u>)<br>An object literal of specific Geocoder options.The syntax should respect the javascript object notation (json) format.<br>As suggested in the field placeholder, always use double quotes (") both for the indexes and the string values.'),
      '#default_value' => isset($settings['geocoder']['settings']['options']) ? $settings['geocoder']['settings']['options'] : $default_settings['geocoder']['settings']['options'],
      '#placeholder' => '{"googlemaps":{"locale": "it", "region": "it"}, "nominatim":{"locale": "it"}}',
      '#element_validate' => [
        [
          get_class($this),
          'jsonValidate',
        ],
      ],
    ];
    if (isset($this->fieldDefinition)) {
      $element['geocoder']['settings']['#states'] = [
        'visible' => [
          ':input[name="fields[' . $this->fieldDefinition
            ->getName() . '][settings_edit_form][settings][geocoder][control]"]' => [
            'checked' => TRUE,
          ],
        ],
      ];
    }
    else {
      $element['geocoder']['settings']['#states'] = [
        'visible' => [
          ':input[name="style_options[geocoder][control]"]' => [
            'checked' => TRUE,
          ],
        ],
      ];
    }
  }
  else {
    $element['geocoder'] = [
      '#markup' => $this
        ->t('<strong>Note: </strong>it is possible to enable a <u>Geocoder controller on the Leaflet Map</u> throughout the @geocoder_module_link integration (version higher than 8.x-2.3 and 8.x-3.0-alpha2).', [
        '@geocoder_module_link' => $this->link
          ->generate('Geocoder Module', Url::fromUri('https://www.drupal.org/project/geocoder', [
          'attributes' => [
            'target' => 'blank',
          ],
        ])),
      ]),
    ];
  }
}