You are here

public static function GeofieldMapFormElementsValidationTrait::zoomLevelValidate in Geofield Map 8.2

Form element validation handler for a Map Zoom level.

File

src/GeofieldMapFormElementsValidationTrait.php, line 24

Class

GeofieldMapFormElementsValidationTrait
Class GeofieldMapFormElementsValidationTrait.

Namespace

Drupal\geofield_map

Code

public static function zoomLevelValidate($element, FormStateInterface &$form_state) {

  // Get to the actual values in a form tree.
  $parents = $element['#parents'];
  $values = $form_state
    ->getValues();
  for ($i = 0; $i < count($parents) - 1; $i++) {
    $values = $values[$parents[$i]];
  }

  // Check the initial map zoom level.
  $zoom = $element['#value'];
  $min_zoom = $values['min'];
  $max_zoom = $values['max'];
  if ($zoom < $min_zoom || $zoom > $max_zoom) {
    $form_state
      ->setError($element, t('The @zoom_field should be between the Minimum and the Maximum Zoom levels.', [
      '@zoom_field' => $element['#title'],
    ]));
  }
}