class GeolocationInput in Geolocation Field 8.2
Same name and namespace in other branches
- 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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\geolocation\Element\GeolocationInput
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
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\ElementView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormElement:: |
public static | function | Adds autocomplete functionality to elements. | |
FormElement:: |
public static | function | #process callback for #pattern form element property. | |
FormElement:: |
public static | function | #element_validate callback for #pattern form element property. | |
FormElement:: |
public static | function |
Determines how user input is mapped to an element's #value property. Overrides FormElementInterface:: |
15 |
GeolocationInput:: |
public | function |
Returns the element properties for this element. Overrides ElementInterface:: |
|
GeolocationInput:: |
public static | function | Processes the geolocation form element. | |
GeolocationInput:: |
public static | function | Form element validation handler for #type 'email'. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
RenderElement:: |
public static | function | Adds Ajax information about an element to communicate with JavaScript. | |
RenderElement:: |
public static | function | Adds members of this group as actual elements for rendering. | |
RenderElement:: |
public static | function | Form element processing handler for the #ajax form property. | 1 |
RenderElement:: |
public static | function | Arranges elements into groups. | |
RenderElement:: |
public static | function |
Sets a form element's class attribute. Overrides ElementInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |