You are here

function webform_preprocess_fieldset in Webform 8.5

Same name and namespace in other branches
  1. 6.x includes/webform.theme.inc \webform_preprocess_fieldset()

Implements hook_preprocess_fieldset() for fieldset templates.

File

includes/webform.theme.inc, line 434
Theme hooks, preprocessor, and suggestions.

Code

function webform_preprocess_fieldset(&$variables) {
  if (!WebformElementHelper::isWebformElement($variables['element'])) {
    return;
  }

  // Setup description, help, and more.
  _webform_preprocess_element($variables, [
    'legend',
    'title',
  ]);
  $element =& $variables['element'];

  // @todo [Drupal 9.3] Remove the below code
  // Fieldset elements now respect the #description_display option
  // @see https://www.drupal.org/node/3143489
  //
  // If the description is displayed 'before' we need to move it to the
  // fieldset's prefix.
  // @see fieldset.html.twig
  if (isset($element['#description_display']) && $element['#description_display'] === 'before' && !empty($variables['description']['content'])) {
    if (isset($variables['prefix'])) {
      if (is_array($variables['prefix'])) {
        $variables['prefix'] = [
          'prefix' => $variables['prefix'],
        ];
      }
      else {
        $variables['prefix'] = [
          'prefix' => [
            '#markup' => $variables['prefix'],
          ],
        ];
      }
    }
    else {
      $variables['prefix'] = [];
    }
    $variables['prefix']['description'] = $variables['description']['content'];
    unset($variables['description']['content']);
  }

  // Apply inline title defined by radios, checkboxes, and buttons.
  // @see \Drupal\webform\Plugin\WebformElement\OptionsBase::prepare
  if (isset($element['#_title_display'])) {
    $variables['attributes']['class'][] = 'webform-fieldset--title-inline';
  }

  // If the title display is none remove the legend.title and set
  // display to none.
  if (isset($element['#title_display']) && $element['#title_display'] === 'none') {
    $variables['legend'] = [
      'attributes' => new Attribute([
        'style' => 'display:none',
      ]),
    ];
  }

  // Add .js-webform-form-type-* class to be used JavaScript and #states API.
  // @see js/webform.element.location.geocomplete.js
  // @see js/webform.states.js
  if (isset($element['#type'])) {
    $variables['attributes']['class'][] = 'js-webform-type-' . Html::getClass($element['#type']);
    $variables['attributes']['class'][] = 'webform-type-' . Html::getClass($element['#type']);
  }

  // Remove invalid 'required' attributes from fieldset.
  //
  // Issue #3174459: W3C Validation: required attribute not allowed on
  // fieldset tag.
  // @see https://www.drupal.org/project/drupal/issues/3174459
  if (isset($element['#webform_key'])) {
    unset($variables['attributes']['required']);
  }
}