You are here

function _webform_preprocess_element in Webform 6.x

Same name and namespace in other branches
  1. 8.5 includes/webform.theme.inc \_webform_preprocess_element()

Prepares webform element description, help, and more templates.

See also

template_preprocess_form_element()

core/modules/system/templates/form-element.html.twig

template_preprocess_details()

/core/modules/system/templates/details.html.twig

template_preprocess_fieldset()

/core/modules/system/templates/fieldset.html.twig

template_preprocess_webform_section()

/webform/templates/webform-section.html.twig

6 calls to _webform_preprocess_element()
template_preprocess_webform_card in modules/webform_cards/webform_cards.module
Prepares variables for webform section element templates.
template_preprocess_webform_section in includes/webform.theme.template.inc
Prepares variables for webform section element templates.
TextFormat::process in src/Plugin/WebformElement/TextFormat.php
Fix text format #more property.
webform_preprocess_details in includes/webform.theme.inc
Implements hook_preprocess_details() for details element templates.
webform_preprocess_fieldset in includes/webform.theme.inc
Implements hook_preprocess_fieldset() for fieldset templates.

... See full list

File

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

Code

function _webform_preprocess_element(array &$variables, $title_parents = [
  'title',
]) {
  $element =& $variables['element'];
  $type = isset($element['#type']) ? $element['#type'] : '';

  // Fix details 'description' property which does not have description.content.
  // @see template_preprocess_details
  // @see Issue #2896169: Details elements have incorrect aria-describedby attributes
  if (!empty($element['#description'])) {

    // Normalize description into a simple render array.
    if (is_array($element['#description'])) {
      $description = [
        $element['#description'],
      ];
    }
    else {
      $description = [
        '#markup' => $element['#description'],
      ];
    }
    if ($type === 'details') {
      $description_attributes = [];
      if (!empty($element['#id'])) {
        $description_attributes['id'] = $element['#id'] . '--description';
      }
      $variables['description'] = [];
      $variables['description']['content'] = [
        '#type' => 'container',
        '#attributes' => new Attribute($description_attributes),
      ] + $description;
    }
    else {
      $variables['description'] += [
        'attributes' => new Attribute(),
      ];

      // Wrap description in a container.
      $variables['description']['content'] = [
        '#type' => 'container',
        '#attributes' => $variables['description']['attributes'],
      ] + $description;
      $variables['description']['attributes'] = new Attribute();
    }
    $variables['description']['content']['#attributes']
      ->addClass('webform-element-description');

    // Handle invisible descriptions.
    if (isset($element['#description_display']) && $element['#description_display'] === 'invisible') {
      $variables['description']['content']['#attributes']
        ->addClass('visually-hidden');
      $variables['description_display'] = 'after';
    }

    // Nest description content so that we can a more link
    // below the description.
    $variables['description']['content'] = [
      'description' => $variables['description']['content'],
    ];
  }
  elseif (isset($variables['description']) && empty($variables['description'])) {

    // Unset $variable['description'] which can be set to NULL or empty string.
    // This allows $variable['description'] to be converted to render array.
    // @see template_preprocess_details()
    // @see template_preprocess_form_element()
    unset($variables['description']);
  }

  // Move #description to #help for webform admin routes.
  _webform_preprocess_description_help($variables);

  // Add (read) more to #description.
  _webform_preprocess_form_element_description_more($variables);

  // Add help to title (aka label).
  _webform_preprocess_help($variables, $title_parents);

  // Hide 'item' label[for].
  if ($type === 'item') {
    $variables['label']['#attributes']['webform-remove-for-attribute'] = TRUE;
  }
}