You are here

function _webform_preprocess_help in Webform 6.x

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

Append #help to title or element variable.

1 call to _webform_preprocess_help()
_webform_preprocess_element in includes/webform.theme.inc
Prepares webform element description, help, and more templates.

File

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

Code

function _webform_preprocess_help(array &$variables, $title_parents = [
  'title',
]) {
  $element =& $variables['element'];
  $type = isset($element['#type']) ? $element['#type'] : '';
  if (empty($element['#help'])) {
    return;
  }
  $help_display = isset($element['#help_display']) ? $element['#help_display'] : 'title_after';

  // Determine target variable (aka render element).
  $targets = [
    'title_before' => 'title',
    'title_after' => 'title',
    // Details don't support prefix and suffix.
    // @see details.html.twig
    'element_before' => $type === 'details' ? 'children' : 'prefix',
    'element_after' => $type === 'details' ? 'children' : 'suffix',
  ];
  $target = $targets[$help_display];

  // Determine the target element.
  if ($target === 'title') {

    // User title parent to the title (target) element.
    $target_element =& NestedArray::getValue($variables, $title_parents);

    // Empty title should not display help.
    if (empty($target_element)) {
      return;
    }
  }
  else {
    $variables += [
      $target => [],
    ];
    $target_element =& $variables[$target];
  }

  // Default #help_title to element's #title.
  if (empty($element['#help_title']) && !empty($element['#title'])) {
    $element['#help_title'] = $element['#title'];
  }
  $build = [];
  if (!empty($target_element)) {
    $build[$target] = is_array($target_element) ? $target_element : [
      '#markup' => $target_element,
    ];
  }
  $build['help'] = [
    '#type' => 'webform_help',
  ] + array_intersect_key($element, array_flip([
    '#help',
    '#help_title',
  ]));

  // Add help attributes.
  if (isset($element['#help_attributes'])) {
    $build['help']['#attributes'] = $element['#help_attributes'];
  }

  // Get #title_display and move help before title for 'inline' titles.
  if (isset($element['#_title_display'])) {

    // #_title_display is set via WebformElementBase::prepare.
    // @see \Drupal\webform\Plugin\WebformElementBase::prepare.
    $title_display = $element['#_title_display'];
  }
  elseif (isset($element['#title_display'])) {
    $title_display = $element['#title_display'];
  }
  else {
    $title_display = NULL;
  }

  // Place help before the target.
  if (isset($build[$target])) {
    if ($target === 'title' && $title_display === 'inline' || $help_display === 'title_before' || $help_display === 'element_before') {
      $build[$target]['#weight'] = 0;
      $build['help']['#weight'] = -1;
    }
  }

  // Add help container classes to element wrapper.
  $variables['attributes']['class'][] = Html::getClass('webform-element-help-container--' . preg_replace('/(_after|_before)/', '', $help_display));
  $variables['attributes']['class'][] = Html::getClass('webform-element-help-container--' . $help_display);

  // Replace $variables with new render array containing help.
  $target_element = $build;
}