View source
<?php
namespace Drupal\geofield\Element;
use Drupal\Core\Form\FormStateInterface;
class GeofieldBounds extends GeofieldElementBase {
public static function getComponents() {
return [
'top' => [
'title' => t('Top'),
'range' => 90,
],
'right' => [
'title' => t('Right'),
'range' => 180,
],
'bottom' => [
'title' => t('Bottom'),
'range' => 90,
],
'left' => [
'title' => t('Left'),
'range' => 180,
],
];
}
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#process' => [
[
$class,
'elementProcess',
],
],
'#element_validate' => [
[
$class,
'boundsValidate',
],
],
'#theme' => 'geofield_bounds',
'#theme_wrappers' => [
'fieldset',
],
];
}
public static function boundsValidate(array &$element, FormStateInterface $form_state, array &$complete_form) {
static::elementValidate($element, $form_state, $complete_form);
$pairs = [
[
'bigger' => 'top',
'smaller' => 'bottom',
],
[
'bigger' => 'right',
'smaller' => 'left',
],
];
foreach ($pairs as $pair) {
if ($element[$pair['smaller']]['#value'] > $element[$pair['bigger']]['#value']) {
$components = static::getComponents();
$form_state
->setError($element[$pair['smaller']], t('@title: @component_bigger must be greater than @component_smaller.', [
'@title' => $element['#title'],
'@component_bigger' => $components[$pair['bigger']]['title'],
'@component_smaller' => $components[$pair['smaller']]['title'],
]));
}
}
}
}