You are here

function webform_preprocess_form_element in Webform 6.x

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

Implements hook_preprocess_form_element() for form element templates.

File

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

Code

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

  // Setup description, help, and more.
  _webform_preprocess_element($variables, [
    'label',
    '#title',
  ]);

  // Make sure the #description_display is always applied to account for
  // #more which is placed in the #description.
  // @see template_preprocess_form_element()
  if (isset($variables['description'])) {
    $element =& $variables['element'];
    $variables['description_display'] = $element['#description_display'];
  }

  // Issue #2700439: Description class not added to
  // form-element.html.twig template.
  // @see https://www.drupal.org/project/drupal/issues/2700439
  if (isset($variables['description_display']) && $variables['description_display'] === 'before') {

    // Add system .description class.
    if (isset($variables['description'])) {
      if (empty($variables['description']['attributes'])) {
        $variables['description']['attributes'] = new Attribute();
      }
      $variables['description']['attributes']
        ->addClass('description');
    }
  }

  // Add missing classes to the Claro theme's form elements.
  // @see core/modules/system/templates/form-element.html.twig
  // @see claro/templates/form-element.html.twig
  // @todo Once Claro is stable determine if this code should removed.
  static $is_claro_theme;
  if (!isset($is_claro_theme)) {

    /** @var \Drupal\webform\WebformThemeManagerInterface $theme_manager */
    $theme_manager = \Drupal::service('webform.theme_manager');
    $is_claro_theme = $theme_manager
      ->isActiveTheme('claro');
  }
  if ($is_claro_theme) {

    // Add system .form-type-TYPE class.
    if (!empty($variables['type'])) {
      $variables['attributes']['class'][] = 'form-type-' . Html::getClass($variables['type']);
    }

    // Add system .description class.
    if (isset($variables['description'])) {
      if (empty($variables['description']['attributes'])) {
        $variables['description']['attributes'] = new Attribute();
      }
      $variables['description']['attributes']
        ->addClass('description');
    }
  }
}