View source
<?php
namespace Drupal\webform\Element;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Element\FormElement;
use Drupal\webform\Utility\WebformElementHelper;
abstract class WebformCompositeBase extends FormElement implements WebformCompositeInterface {
use WebformCompositeFormElementTrait;
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#access' => TRUE,
'#process' => [
[
$class,
'processWebformComposite',
],
[
$class,
'processAjaxForm',
],
],
'#pre_render' => [
[
$class,
'preRenderWebformCompositeFormElement',
],
],
'#title_display' => 'invisible',
'#required' => FALSE,
'#flexbox' => TRUE,
];
}
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
$element_manager = \Drupal::service('plugin.manager.webform.element');
$composite_elements = static::getCompositeElements($element);
$composite_elements = WebformElementHelper::getFlattened($composite_elements);
$default_value = [];
foreach ($composite_elements as $composite_key => $composite_element) {
$element_plugin = $element_manager
->getElementInstance($composite_element);
if ($element_plugin
->isInput($composite_element)) {
$default_value[$composite_key] = '';
}
}
if ($input === FALSE) {
if (empty($element['#default_value']) || !is_array($element['#default_value'])) {
$element['#default_value'] = [];
}
return $element['#default_value'] + $default_value;
}
return is_array($input) ? $input + $default_value : $default_value;
}
public static function preRenderCompositeFormElement($element) {
$element['#theme_wrappers'][] = 'form_element';
$element['#wrapper_attributes']['id'] = $element['#id'] . '--wrapper';
$element['#wrapper_attributes']['class'][] = 'form-composite';
$element['#attributes']['id'] = $element['#id'];
$class_name = str_replace('_', '-', $element['#type']);
static::setAttributes($element, [
'js-' . $class_name,
$class_name,
]);
return $element;
}
public static function processWebformComposite(&$element, FormStateInterface $form_state, &$complete_form) {
if (isset($element['#initialize'])) {
return $element;
}
$element['#initialize'] = TRUE;
$element['#tree'] = TRUE;
$composite_elements = static::initializeCompositeElements($element);
static::processWebformCompositeElementsRecursive($element, $composite_elements, $form_state, $complete_form);
$element += $composite_elements;
$element += [
'#element_validate' => [],
];
array_unshift($element['#element_validate'], [
get_called_class(),
'validateWebformComposite',
]);
if (!empty($element['#flexbox'])) {
$element['#attached']['library'][] = 'webform/webform.element.flexbox';
}
return $element;
}
public static function processWebformCompositeElementsRecursive(&$element, array &$composite_elements, FormStateInterface $form_state, &$complete_form) {
$element_manager = \Drupal::service('plugin.manager.webform.element');
$composite_required_states = WebformElementHelper::getRequiredFromVisibleStates($element);
foreach ($composite_elements as $composite_key => &$composite_element) {
$composite_key = (string) $composite_key;
if (!Element::child($composite_key) || !is_array($composite_element)) {
continue;
}
$composite_element['#parents'] = array_merge($element['#parents'], [
$composite_key,
]);
if ($element['#access'] === FALSE) {
$composite_element['#access'] = FALSE;
}
$element_plugin = $element_manager
->getElementInstance($composite_element);
if ($element_plugin
->isInput($composite_element)) {
if (isset($element['#value'][$composite_key])) {
$composite_element['#default_value'] = $element['#value'][$composite_key];
}
}
$element_manager
->buildElement($composite_element, $complete_form, $form_state);
if ($composite_required_states && !empty($composite_element['#required'])) {
unset($composite_element['#required']);
$composite_element['#_required'] = TRUE;
if (!isset($composite_element['#states'])) {
$composite_element['#states'] = [];
}
$composite_element['#states'] += $composite_required_states;
}
static::processWebformCompositeElementsRecursive($element, $composite_element, $form_state, $complete_form);
}
}
public static function validateWebformComposite(&$element, FormStateInterface $form_state, &$complete_form) {
$value = NestedArray::getValue($form_state
->getValues(), $element['#parents']);
if (Element::isVisibleElement($element)) {
$composite_elements = static::getCompositeElements($element);
$composite_elements = WebformElementHelper::getFlattened($composite_elements);
foreach ($composite_elements as $composite_key => $composite_element) {
$is_required = !empty($element[$composite_key]['#required']);
$is_empty = isset($value[$composite_key]) && $value[$composite_key] === '';
if ($is_required && $is_empty) {
WebformElementHelper::setRequiredError($element[$composite_key], $form_state);
}
}
}
if (empty(array_filter($value))) {
$element['#value'] = NULL;
$form_state
->setValueForElement($element, NULL);
}
}
public static function getCompositeElements(array $element) {
return [];
}
public static function initializeCompositeElements(array &$element) {
$composite_elements = static::getCompositeElements($element);
static::initializeCompositeElementsRecursive($element, $composite_elements);
return $composite_elements;
}
protected static function initializeCompositeElementsRecursive(array &$element, array &$composite_elements) {
$element_manager = \Drupal::service('plugin.manager.webform.element');
foreach ($composite_elements as $composite_key => &$composite_element) {
if (WebformElementHelper::property($composite_key)) {
continue;
}
foreach ($element as $property_key => $property_value) {
if (strpos($property_key, '#' . $composite_key . '__') === 0) {
$composite_property_key = str_replace('#' . $composite_key . '__', '#', $property_key);
$composite_element[$composite_property_key] = $property_value;
}
}
$element_plugin = $element_manager
->getElementInstance($composite_element);
if (isset($composite_element['#options']) && !$element_plugin
->hasProperty('options')) {
unset($composite_element['#options']);
}
if (isset($composite_element['#placeholder']) && $element_plugin
->hasProperty('empty_option')) {
$composite_element['#empty_option'] = $composite_element['#placeholder'];
}
if (isset($composite_element['#type']) && strpos($composite_element['#type'], 'select') !== FALSE) {
$select_properties = [
'#select2' => '#select2',
'#choices' => '#choices',
'#chosen' => '#chosen',
];
$composite_element += array_intersect_key($element, $select_properties);
}
if ($element_plugin
->hasMultipleValues($composite_element)) {
throw new \Exception('Multiple elements are not supported within composite elements.');
}
if ($element_plugin
->isComposite()) {
throw new \Exception('Nested composite elements are not supported within composite elements.');
}
$element_plugin
->initialize($composite_element);
static::initializeCompositeElementsRecursive($element, $composite_element);
}
}
}