function template_preprocess_fieldset

Same name in other branches
  1. 8.9.x core/includes/form.inc \template_preprocess_fieldset()
  2. 10 core/includes/form.inc \template_preprocess_fieldset()
  3. 11.x core/includes/form.inc \template_preprocess_fieldset()

Prepares variables for fieldset element templates.

Default template: fieldset.html.twig.

Parameters

array $variables: An associative array containing:

  • element: An associative array containing the properties of the element. Properties used: #attributes, #children, #description, #id, #title, #value.

File

core/includes/form.inc, line 192

Code

function template_preprocess_fieldset(&$variables) {
    $element = $variables['element'];
    Element::setAttributes($element, [
        'id',
    ]);
    RenderElement::setAttributes($element);
    $variables['attributes'] = $element['#attributes'] ?? [];
    $variables['prefix'] = $element['#field_prefix'] ?? NULL;
    $variables['suffix'] = $element['#field_suffix'] ?? NULL;
    $variables['title_display'] = $element['#title_display'] ?? NULL;
    $variables['children'] = $element['#children'];
    $variables['required'] = !empty($element['#required']) ? $element['#required'] : NULL;
    if (isset($element['#title']) && $element['#title'] !== '') {
        $variables['legend']['title'] = [
            '#markup' => $element['#title'],
        ];
    }
    $variables['legend']['attributes'] = new Attribute();
    // Add 'visually-hidden' class to legend span.
    if ($variables['title_display'] == 'invisible') {
        $variables['legend_span']['attributes'] = new Attribute([
            'class' => [
                'visually-hidden',
            ],
        ]);
    }
    else {
        $variables['legend_span']['attributes'] = new Attribute();
    }
    if (!empty($element['#description'])) {
        $description_id = $element['#attributes']['id'] . '--description';
        $description_attributes['id'] = $description_id;
        $variables['description_display'] = $element['#description_display'];
        if ($element['#description_display'] === 'invisible') {
            $description_attributes['class'][] = 'visually-hidden';
        }
        $description_attributes['data-drupal-field-elements'] = 'description';
        $variables['description']['attributes'] = new Attribute($description_attributes);
        $variables['description']['content'] = $element['#description'];
        // Add the description's id to the fieldset aria attributes.
        $variables['attributes']['aria-describedby'] = $description_id;
    }
    // Suppress error messages.
    $variables['errors'] = NULL;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.