You are here

class GeolocationInput in Geolocation Field 8.2

Same name and namespace in other branches
  1. 8.3 src/Element/GeolocationInput.php \Drupal\geolocation\Element\GeolocationInput

Provides a render element to display a geolocation map.

Usage example:

$form['map'] = [
  '#type' => 'geolocation_input',
  '#prefix' => $this
    ->t('Geolocation Input'),
  '#description' => $this
    ->t('Form element type "geolocation_input"'),
];

Plugin annotation

@FormElement("geolocation_input");

Hierarchy

Expanded class hierarchy of GeolocationInput

4 #type uses of GeolocationInput
GeolocationBlock::blockForm in src/Plugin/Block/GeolocationBlock.php
GeolocationMapWidgetBase::formElement in src/Plugin/Field/FieldWidget/GeolocationMapWidgetBase.php
Returns the form for a single field widget.
LocationInputBase::getForm in src/LocationInputBase.php
Get center form.
Photon::getOptionsForm in modules/geolocation_leaflet/src/Plugin/geolocation/Geocoder/Photon.php
Return additional options form.

File

src/Element/GeolocationInput.php, line 22

Namespace

Drupal\geolocation\Element
View source
class GeolocationInput extends FormElement {

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    return [
      '#input' => TRUE,
      '#default_value' => NULL,
      '#process' => [
        [
          $class,
          'processGeolocation',
        ],
        [
          $class,
          'processGroup',
        ],
      ],
      '#pre_render' => [
        [
          $class,
          'preRenderGroup',
        ],
      ],
      '#element_validate' => [
        [
          $class,
          'validateGeolocation',
        ],
      ],
      '#theme_wrappers' => [
        'fieldset',
      ],
    ];
  }

  /**
   * Processes the geolocation form element.
   *
   * @param array $element
   *   The form element to process.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param array $complete_form
   *   The complete form structure.
   *
   * @return array
   *   The processed element.
   */
  public static function processGeolocation(array &$element, FormStateInterface $form_state, array &$complete_form) {
    $default_field_values = [
      'lat' => '',
      'lng' => '',
    ];
    if ($element['#defaults_loaded'] && isset($element['#value']['lat']) && isset($element['#value']['lng'])) {
      $default_field_values = [
        'lat' => $element['#value']['lat'],
        'lng' => $element['#value']['lng'],
      ];
    }
    elseif (!empty($element['#default_value']) && isset($element['#default_value']['lat']) && isset($element['#default_value']['lng'])) {
      $default_field_values = [
        'lat' => $element['#default_value']['lat'],
        'lng' => $element['#default_value']['lng'],
      ];
    }
    $element['lat'] = [
      '#type' => 'textfield',
      '#title' => t('Latitude'),
      '#default_value' => $default_field_values['lat'],
      '#attributes' => [
        'class' => [
          'geolocation-input-latitude',
          'geolocation-input-latitude',
        ],
      ],
    ];
    $element['lng'] = [
      '#type' => 'textfield',
      '#title' => t('Longitude'),
      '#default_value' => $default_field_values['lng'],
      '#attributes' => [
        'class' => [
          'geolocation-input-longitude',
        ],
      ],
    ];
    if (empty($element['#wrapper_attributes'])) {
      $element['#wrapper_attributes'] = [];
    }
    $element['#wrapper_attributes'] = array_merge_recursive($element['#wrapper_attributes'], [
      'class' => [
        'geolocation-input',
      ],
    ]);
    return $element;
  }

  /**
   * Form element validation handler for #type 'email'.
   *
   * @param array $element
   *   The form element to process.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param array $complete_form
   *   The complete form structure.
   */
  public static function validateGeolocation(array &$element, FormStateInterface $form_state, array &$complete_form) {
    if (empty($element['#value']['lng']) && empty($element['#value']['lat'])) {
      return;
    }
    if (!is_numeric($element['#value']['lng'])) {
      $form_state
        ->setError($element, t('Longitude not numeric.'));
    }
    if (!is_numeric($element['#value']['lat'])) {
      $form_state
        ->setError($element, t('Latitude not numeric.'));
    }
    $longitude = floatval($element['#value']['lng']);
    $latitude = floatval($element['#value']['lat']);
    if ($latitude < -90 || $latitude > 90) {
      $form_state
        ->setError($element, t('Latitude must be between -90 and 90.'));
    }
    if ($longitude < -180 || $longitude > 180) {
      $form_state
        ->setError($element, t('Longitude must be between -180 and 180.'));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormElement::processAutocomplete public static function Adds autocomplete functionality to elements.
FormElement::processPattern public static function #process callback for #pattern form element property.
FormElement::validatePattern public static function #element_validate callback for #pattern form element property.
FormElement::valueCallback public static function Determines how user input is mapped to an element's #value property. Overrides FormElementInterface::valueCallback 15
GeolocationInput::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
GeolocationInput::processGeolocation public static function Processes the geolocation form element.
GeolocationInput::validateGeolocation public static function Form element validation handler for #type 'email'.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.