View source
<?php
namespace Drupal\webform\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
abstract class WebformLocationBase extends WebformCompositeBase {
protected static $name;
public function getInfo() {
return parent::getInfo() + [
'#theme' => 'webform_composite_location',
'#map' => FALSE,
'#geolocation' => FALSE,
'#hidden' => FALSE,
];
}
public static function getLocationAttributes() {
return [];
}
public static function getCompositeElements(array $element) {
$elements = [];
$elements['value'] = [
'#type' => 'textfield',
'#title' => t('Address'),
'#attributes' => [
'class' => [
'webform-location-' . static::$name,
],
],
];
$attributes = static::getLocationAttributes();
foreach ($attributes as $name => $title) {
$elements[$name] = [
'#title' => $title,
'#type' => 'textfield',
'#error_no_message' => TRUE,
'#attributes' => [
'data-webform-location-' . static::$name . '-attribute' => $name,
],
];
}
return $elements;
}
public static function preRenderWebformCompositeFormElement($element) {
if (!empty($element['#hidden']) && !empty($element['#geolocation'])) {
$element['#wrapper_attributes']['style'] = 'display: none';
}
$element = WebformCompositeBase::preRenderWebformCompositeFormElement($element);
return $element;
}
public static function processWebformComposite(&$element, FormStateInterface $form_state, &$complete_form) {
$element = parent::processWebformComposite($element, $form_state, $complete_form);
$composite_elements = static::getCompositeElements($element);
foreach ($composite_elements as $composite_key => $composite_element) {
if ($composite_key !== 'value') {
if (!Element::isVisibleElement($element[$composite_key])) {
unset($element[$composite_key]['#access']);
unset($element[$composite_key]['#pre_render']);
$element[$composite_key]['#type'] = 'hidden';
}
elseif (!empty($element['#hidden']) && !empty($element['#geolocation'])) {
unset($element[$composite_key]['#pre_render']);
$element[$composite_key]['#type'] = 'hidden';
}
else {
$element[$composite_key]['#wrapper_attributes']['class'][] = 'webform-readonly';
$element[$composite_key]['#readonly'] = 'readonly';
}
}
}
$shared_properties = [
'#required',
'#placeholder',
];
$element['value'] += array_intersect_key($element, array_combine($shared_properties, $shared_properties));
if (!empty($element['#geolocation'])) {
$element['value']['#attributes']['data-webform-location-' . static::$name . '-geolocation'] = 'data-webform-location-' . static::$name . '-geolocation';
}
if (!empty($element['#map']) && empty($element['#hidden'])) {
$element['value']['#attributes']['data-webform-location-' . static::$name . '-map'] = 'data-webform-location-' . static::$name . '-map';
}
$element += [
'#element_validate' => [],
];
array_unshift($element['#element_validate'], [
get_called_class(),
'validateWebformLocation',
]);
return $element;
}
public static function validateWebformLocation(&$element, FormStateInterface $form_state, &$complete_form) {
$value = $element['#value'];
if (Element::isVisibleElement($element) && !empty($element['#required']) && empty($value['lat'])) {
$t_args = [
'@title' => !empty($element['#title']) ? $element['#title'] : t('Location'),
];
$form_state
->setError($element['value'], t('The @title is not valid.', $t_args));
}
}
}