You are here

public function IpGeoLocPluginStyleLeaflet::validateOptionsForm in IP Geolocation Views & Maps 8

Validate the options form.

Overrides StylePluginBase::validateOptionsForm

File

src/Plugin/views/style/IpGeoLocPluginStyleLeaflet.php, line 1055

Class

IpGeoLocPluginStyleLeaflet
Views Style plugin extension for Leaflet (if enabled).

Namespace

Drupal\ip_geoloc\Plugin\views\style

Code

public function validateOptionsForm(&$form, FormStateInterface $form_state) {
  $this->viewPluginStyle
    ->pluginStyleBulkOfFormValidate($form, $form_state);
  $this_options = $form_state
    ->getValue('style_options');
  $map_height = trim($this_options['map_height']);
  if (is_numeric($map_height) && $map_height <= 0) {
    form_error($form['map_height'], $this
      ->t('Map height must be a positive number.'));
  }
  $selected_map = $this
    ->pluginStyleLeafletMapGetInfo($this_options['map']);
  $zoom_top = 18;
  if (isset($selected_map['settings']['maxZoom'])) {
    $zoom_top = $selected_map['settings']['maxZoom'];
  }
  $max_zoom = $this_options['map_options_leaflet']['maxzoom'];
  if ($max_zoom != '' && (!is_numeric($max_zoom) || $max_zoom < 0 || $max_zoom > $zoom_top)) {
    $form_state
      ->setError($form['map_options_leaflet']['maxzoom'], $this
      ->t('"Maximum zoom level" for %map must be in range 0..@zoomtop', [
      '%map' => $selected_map['label'],
      '@zoomtop' => $zoom_top,
    ]));
  }
  $zoom = $this_options['map_options_leaflet']['zoom'];
  if ($zoom != '' && (!is_numeric($zoom) || $zoom < 0 || $zoom > $max_zoom)) {
    $form_state
      ->setError($form['map_options_leaflet']['zoom'], $this
      ->t('"Initial zoom level" must be a non-negative number not greater than "Maximum zoom level".'));
  }
  $disable_zoom = $this_options['disable_clustering_at_zoom'];
  if ($disable_zoom != '' && (!is_numeric($disable_zoom) || $disable_zoom < 0 || $disable_zoom > $max_zoom)) {
    $form_state
      ->setError($form['disable_clustering_at_zoom'], $this
      ->t('"Disable clustering at zoom" level must be a positive number not greater than "Maximum zoom level".'));
  }
  if (isset($this_options['cluster_aggregation'])) {
    $cluster_aggregation = $this_options['cluster_aggregation'];
    if (!empty($cluster_aggregation['aggregation_field'])) {
      $aggregation_field_info = FieldStorageConfig::loadByName($cluster_aggregation['aggregation_field']);
      $valid_types = [
        'number_integer',
        'number_decimal',
        'number_float',
        'entityreference',
      ];
      if (!$aggregation_field_info || !in_array($aggregation_field_info['type'], $valid_types)) {
        $this->messenger
          ->addMessage($this
          ->t('A cluster aggregation field that cannot be interpreted as a number may cause unexpected errors.'));
      }
    }
  }
}