You are here

trait GeofieldMapFormElementsValidationTrait in Geofield Map 8.2

Class GeofieldMapFormElementsValidationTrait.

Provide common functions for Geofield Map elements validations.

@package Drupal\geofield_map

Hierarchy

4 files declare their use of GeofieldMapFormElementsValidationTrait
GeofieldGoogleEmbedMapFormatter.php in modules/geofield_map_extras/src/Plugin/Field/FieldFormatter/GeofieldGoogleEmbedMapFormatter.php
GeofieldGoogleMapFormatter.php in src/Plugin/Field/FieldFormatter/GeofieldGoogleMapFormatter.php
GeofieldGoogleMapViewStyle.php in src/Plugin/views/style/GeofieldGoogleMapViewStyle.php
GeofieldMapWidget.php in src/Plugin/Field/FieldWidget/GeofieldMapWidget.php

File

src/GeofieldMapFormElementsValidationTrait.php, line 17

Namespace

Drupal\geofield_map
View source
trait GeofieldMapFormElementsValidationTrait {

  /**
   * Form element validation handler for a Map Zoom level.
   *
   * {@inheritdoc}
   */
  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'],
      ]));
    }
  }

  /**
   * Form element validation handler for the Map Max Zoom level.
   *
   * {@inheritdoc}
   */
  public static function maxZoomLevelValidate($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 max zoom level.
    $min_zoom = $values['min'];
    $max_zoom = $element['#value'];
    if ($max_zoom && $max_zoom <= $min_zoom) {
      $form_state
        ->setError($element, t('The Max Zoom level should be above the Minimum Zoom level.'));
    }
  }

  /**
   * Form element validation handler for a Custom Map Style Name Required.
   *
   * {@inheritdoc}
   */
  public static function customMapStyleValidate($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]];
    }
    if ($values['custom_style_control'] && empty($element['#value'])) {
      $form_state
        ->setError($element, t('The @field cannot be empty.', [
        '@field' => $element['#title'],
      ]));
    }
  }

  /**
   * Form element json format validation handler.
   *
   * {@inheritdoc}
   */
  public static function jsonValidate($element, FormStateInterface &$form_state) {
    $element_values_array = Json::decode($element['#value']);

    // Check the jsonValue.
    if (!empty($element['#value']) && $element_values_array == NULL) {
      $form_state
        ->setError($element, t('The @field field is not valid Json Format.', [
        '@field' => $element['#title'],
      ]));
    }
    elseif (!empty($element['#value'])) {
      $form_state
        ->setValueForElement($element, Json::encode($element_values_array));
    }
  }

  /**
   * Form element url format validation handler.
   *
   * {@inheritdoc}
   */
  public static function urlValidate($element, FormStateInterface &$form_state) {
    $path = $element['#value'];

    // Check the jsonValue.
    if (UrlHelper::isExternal($path) && !UrlHelper::isValid($path, TRUE)) {
      $form_state
        ->setError($element, t('The @field field is not valid Url Format.', [
        '@field' => $element['#title'],
      ]));
    }
    elseif (!UrlHelper::isExternal($path)) {
      $path = Url::fromUri('base:' . $path, [
        'absolute' => TRUE,
      ])
        ->toString();
      if (!UrlHelper::isValid($path)) {
        $form_state
          ->setError($element, t('The @field field is not valid internal Drupal path.', [
          '@field' => $element['#title'],
        ]));
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GeofieldMapFormElementsValidationTrait::customMapStyleValidate public static function Form element validation handler for a Custom Map Style Name Required.
GeofieldMapFormElementsValidationTrait::jsonValidate public static function Form element json format validation handler.
GeofieldMapFormElementsValidationTrait::maxZoomLevelValidate public static function Form element validation handler for the Map Max Zoom level.
GeofieldMapFormElementsValidationTrait::urlValidate public static function Form element url format validation handler.
GeofieldMapFormElementsValidationTrait::zoomLevelValidate public static function Form element validation handler for a Map Zoom level.