You are here

protected function GeofieldMapFieldTrait::setGeocoderMapControl in Geofield Map 8.2

Set Map Geocoder Control Element.

Parameters

array $element: The Form element to alter.

array $settings: The Form Settings.

2 calls to GeofieldMapFieldTrait::setGeocoderMapControl()
GeofieldMapFieldTrait::generateGmapSettingsForm in src/GeofieldMapFieldTrait.php
Generate the Google Map Settings Form.
GeofieldMapWidget::settingsForm in src/Plugin/Field/FieldWidget/GeofieldMapWidget.php
Returns a form to configure settings for the widget.

File

src/GeofieldMapFieldTrait.php, line 1390

Class

GeofieldMapFieldTrait
Class GeofieldMapFieldTrait.

Namespace

Drupal\geofield_map

Code

protected function setGeocoderMapControl(array &$element, array $settings) {
  $geocoder_module_link = $this->link
    ->generate('Geocoder Module', Url::fromUri('https://www.drupal.org/project/geocoder', [
    'attributes' => [
      'target' => 'blank',
    ],
  ]));
  $element['map_geocoder'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->getMapGeocoderTitle(),
  ];

  // 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();
    $map_geocoder_control = isset($settings['map_geocoder']) ? $settings['map_geocoder']['control'] : FALSE;
    $element['map_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['map_geocoder']['control'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable @search_address_geocoder', [
        '@search_address_geocoder' => $element['map_geocoder']['#title'],
      ]),
      '#description' => $this
        ->t('This will add a Geocoder control element to the Geofield Map'),
      '#default_value' => $map_geocoder_control ?: $default_settings['map_geocoder']['control'],
    ];
    $element['map_geocoder']['settings'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Geocoder Settings'),
    ];
    $element['map_geocoder']['settings']['position'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Position'),
      '#options' => $this->controlPositionsOptions,
      '#default_value' => isset($settings['map_geocoder']['settings']['position']) ? $settings['map_geocoder']['settings']['position'] : $default_settings['map_geocoder']['settings']['position'],
    ];
    $element['map_geocoder']['settings']['input_size'] = [
      '#title' => $this
        ->t('Input Size'),
      '#type' => 'number',
      '#min' => 10,
      '#max' => 100,
      '#default_value' => isset($settings['map_geocoder']['settings']['input_size']) ? $settings['map_geocoder']['settings']['input_size'] : $default_settings['map_geocoder']['settings']['input_size'],
      '#description' => $this
        ->t('The characters size/length of the Geocoder Input element.'),
    ];
    $providers_settings = isset($settings['map_geocoder']['settings']['providers']) ? $settings['map_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['map_geocoder']['settings']['providers'] = $geocoder_provider
      ->providersPluginsTableList($enabled_providers);

    // Set a validation for the providers selection.
    $element['map_geocoder']['settings']['providers']['#element_validate'] = [
      [
        get_class($this),
        'validateGeocoderProviders',
      ],
    ];
    $element['map_geocoder']['settings']['min_terms'] = [
      '#type' => 'number',
      '#default_value' => isset($settings['map_geocoder']['settings']['min_terms']) ? $settings['map_geocoder']['settings']['min_terms'] : $default_settings['map_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['map_geocoder']['settings']['delay'] = [
      '#type' => 'number',
      '#default_value' => isset($settings['map_geocoder']['settings']['delay']) ? $settings['map_geocoder']['settings']['delay'] : $default_settings['map_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['map_geocoder']['settings']['zoom'] = [
      '#title' => $this
        ->t('Zoom to Focus'),
      '#type' => 'number',
      '#min' => 1,
      '#max' => 22,
      '#default_value' => isset($settings['map_geocoder']['settings']['zoom']) ? $settings['map_geocoder']['settings']['zoom'] : $default_settings['map_geocoder']['settings']['zoom'],
      '#description' => $this
        ->t('Zoom level to Focus on the Map upon the Geocoder Address selection.'),
    ];
    $element['map_geocoder']['settings']['infowindow'] = [
      '#title' => $this
        ->t('Open infowindow on Geocode Focus'),
      '#type' => 'checkbox',
      '#default_value' => isset($settings['map_geocoder']['settings']['infowindow']) ? $settings['map_geocoder']['settings']['infowindow'] : $default_settings['map_geocoder']['settings']['infowindow'],
      '#description' => $this
        ->t('Check this to open an Infowindow on the Map (with the found Address) upon the Geocode Focus.'),
    ];
    $element['map_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['map_geocoder']['settings']['options']) ? $settings['map_geocoder']['settings']['options'] : $default_settings['map_geocoder']['settings']['options'],
      '#placeholder' => '{"googlemaps":{"locale": "it", "region": "it"}, "nominatim":{"locale": "it"}}',
      '#element_validate' => [
        [
          get_class($this),
          'jsonValidate',
        ],
      ],
    ];
    if (isset($this->fieldDefinition)) {
      $element['map_geocoder']['settings']['#states'] = [
        'visible' => [
          ':input[name="fields[' . $this->fieldDefinition
            ->getName() . '][settings_edit_form][settings][map_geocoder][control]"]' => [
            'checked' => TRUE,
          ],
        ],
      ];
    }
    else {
      $element['map_geocoder']['settings']['#states'] = [
        'visible' => [
          ':input[name="style_options[map_geocoder][control]"]' => [
            'checked' => TRUE,
          ],
        ],
      ];
    }
  }
  else {
    $element['map_geocoder']['enable_warning'] = [
      '#markup' => $this
        ->t('<strong>Note: </strong>it is possible to enable the <u>Search Address input element on the Geofield Map</u> throughout the @geocoder_module_link integration (version higher than 8.x-2.3 and 8.x-3.0-alpha2).', [
        '@geocoder_module_link' => $geocoder_module_link,
      ]),
    ];
  }
}