WebformCompositeFormElementTrait.php in Webform 8.5
File
src/Element/WebformCompositeFormElementTrait.php
View source
<?php
namespace Drupal\webform\Element;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Render\Element;
use Drupal\webform\Utility\WebformArrayHelper;
trait WebformCompositeFormElementTrait {
public static function preRenderWebformCompositeFormElement($element) {
$has_content = isset($element['#title']) || isset($element['#description']);
if (!$has_content) {
return $element;
}
if (!isset($element['#attributes'])) {
$element['#attributes'] = [];
}
if (isset($element['#wrapper_attributes'])) {
$element['#attributes'] = NestedArray::mergeDeep($element['#attributes'], $element['#wrapper_attributes']);
}
if (isset($element['#states']) && isset($element['#attributes']['class'])) {
$element_manager = \Drupal::service('plugin.manager.webform.element');
$element_plugin = $element_manager
->getElementInstance($element);
if ($element_plugin
->getPluginDefinition()['states_wrapper']) {
WebformArrayHelper::removeValue($element['#attributes']['class'], 'js-webform-states-hidden');
}
}
if (!isset($element['#attributes']['id'])) {
$element['#attributes']['id'] = $element['#id'] . '--wrapper';
}
$element['#attributes']['class'][] = Html::getClass($element['#type']) . '--wrapper';
$element['#attributes']['class'][] = 'fieldgroup';
$element['#attributes']['class'][] = 'form-composite';
$element['#attached']['library'][] = 'webform/webform.composite';
$wrapper_type = isset($element['#wrapper_type']) ? $element['#wrapper_type'] : 'fieldset';
$element['#theme_wrappers'][] = $wrapper_type;
switch ($wrapper_type) {
case 'fieldset':
if (isset($element['#title']) && $element['#title_display'] === 'attribute') {
$element['#attributes']['title'] = $element['#title'];
if (!empty($element['#required'])) {
$element['#attributes']['title'] .= ' (' . t('Required') . ')';
}
}
if (isset($element['#title'])) {
if (!empty($element['#title_display']) && in_array($element['#title_display'], [
'invisible',
'attribute',
])) {
$element['#attributes']['class'][] = 'webform-composite-hidden-title';
}
else {
$element['#attributes']['class'][] = 'webform-composite-visible-title';
}
}
break;
case 'form_element':
webform_process_states($element, '#wrapper_attributes');
break;
}
if (!empty($element['#description'])) {
$fix_aria_describedby = preg_match('/^(?:webform_)?(?:radios|checkboxes|buttons)(?:_other)?$/', $element['#type']);
foreach (Element::children($element) as $key) {
if (!empty($element[$key]['#description'])) {
continue;
}
if (empty($element[$key]['#attributes']['aria-describedby'])) {
continue;
}
if ($element[$key]['#attributes']['aria-describedby'] === $element['#id'] . '--description') {
if ($fix_aria_describedby) {
$element[$key]['#attributes']['aria-describedby'] = $element['#attributes']['id'] . '--description';
}
else {
unset($element[$key]['#attributes']['aria-describedby']);
}
}
}
}
return $element;
}
}