3 namespace Drupal\Core\Render\Element;
6 * Provides a trait for radios, checkboxes, and similar composite form elements.
8 * Any form element that is comprised of several distinct parts can use this
9 * trait to add support for a composite title or description.
11 trait CompositeFormElementTrait {
14 * Adds form element theming to an element if its title or description is set.
16 * This is used as a pre render function for checkboxes and radios.
18 public static function preRenderCompositeFormElement($element) {
19 // Set the element's title attribute to show #title as a tooltip, if needed.
20 if (isset($element['#title']) && $element['#title_display'] == 'attribute') {
21 $element['#attributes']['title'] = $element['#title'];
22 if (!empty($element['#required'])) {
23 // Append an indication that this field is required.
24 $element['#attributes']['title'] .= ' (' . t('Required') . ')';
28 if (isset($element['#title']) || isset($element['#description'])) {
29 // @see #type 'fieldgroup'
30 $element['#attributes']['id'] = $element['#id'] . '--wrapper';
31 $element['#theme_wrappers'][] = 'fieldset';
32 $element['#attributes']['class'][] = 'fieldgroup';
33 $element['#attributes']['class'][] = 'form-composite';