You are here

function template_preprocess_webform_element_base_html in Webform 6.x

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

Prepares variables for webform element base HTML templates.

Default template: webform-element-base-html.html.twig.

Parameters

array $variables: An associative array containing the following key:

  • element: The webform element.
  • value: The content for the element.
  • options Associative array of options for element.
    • multiline: Flag to determine if value spans multiple lines.
    • email: Flag to determine if element is for an email.

File

includes/webform.theme.template.inc, line 427
Preprocessors and helper functions to make theming easier.

Code

function template_preprocess_webform_element_base_html(array &$variables) {
  $element = $variables['element'];

  // Set title.
  _template_progress_webform_set_title($variables);

  // Build form (item) element for HTML display.
  // @see form-element.html.twig
  // @see template_preprocess_form_element
  if (empty($variables['options']['email']) && isset($element['#type'])) {
    $type = $element['#type'];
    $attributes = isset($element['#format_attributes']) ? $element['#format_attributes'] : [];
    $attributes += [
      'class' => [],
    ];

    // Use wrapper attributes for the id instead of #id,
    // this stops the <label> from having a 'for' attribute.
    $attributes += [
      'id' => $element['#webform_id'],
    ];
    $attributes['class'][] = 'webform-element';
    $attributes['class'][] = 'webform-element-type-' . str_replace('_', '-', $type);
    $variables['item'] = [
      '#type' => 'item',
      '#title' => $variables['title'],
      '#name' => $element['#webform_key'],
      '#wrapper_attributes' => $attributes,
    ];
    if (is_array($variables['value'])) {
      $variables['item']['value'] = $variables['value'];
    }
    else {
      $variables['item']['#markup'] = $variables['value'];
    }
  }
  else {
    $variables['title'] = [
      '#markup' => $variables['title'],
    ];
  }
}