You are here

function webform_process_options in Webform 8.5

Same name and namespace in other branches
  1. 6.x webform.module \webform_process_options()

Process radios or checkboxes descriptions.

Parameters

array $element: An associative array containing the properties and children of the radios or checkboxes element.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

array $complete_form: The complete webform structure.

Return value

array The processed element.

1 string reference to 'webform_process_options'
webform_element_info_alter in ./webform.module
Implements hook_element_info_alter().

File

./webform.module, line 963
Enables the creation of webforms and questionnaires.

Code

function webform_process_options(&$element, FormStateInterface $form_state, &$complete_form) {
  if (!WebformElementHelper::isWebformElement($element)) {
    return $element;
  }

  // Description display.
  if (!empty($element['#options_description_display'])) {
    $description_property_name = $element['#options_description_display'] === 'help' ? '#help' : '#description';
    foreach (Element::children($element) as $key) {
      $title = (string) $element[$key]['#title'];

      // Check for -- delimiter.
      if (!WebformOptionsHelper::hasOptionDescription($title)) {
        continue;
      }
      list($title, $description) = WebformOptionsHelper::splitOption($title);
      $element[$key]['#title'] = $title;
      $element[$key]['#webform_element'] = TRUE;
      $element[$key][$description_property_name] = $description;
    }
  }

  // Display as buttons.
  if (!empty($element['#options_display']) && strpos($element['#options_display'], 'buttons') === 0) {
    foreach (Element::children($element) as $key) {

      // Add wrapper which is needed to make flexbox work with tables.
      $element[$key]['#prefix'] = '<div class="webform-options-display-buttons-wrapper">';
      $element[$key]['#suffix'] = '</div>';

      // Move radio #description inside the #title (aka label).
      if (!empty($element[$key]['#description'])) {
        $build = [
          'title' => [
            '#markup' => $element[$key]['#title'],
            '#prefix' => '<div class="webform-options-display-buttons-title">',
            '#suffix' => '</div>',
          ],
          'description' => [
            '#markup' => $element[$key]['#description'],
            '#prefix' => '<div class="webform-options-display-buttons-description description">',
            '#suffix' => '</div>',
          ],
        ];
        $element[$key]['#title'] = \Drupal::service('renderer')
          ->render($build);
        unset($element[$key]['#description']);
      }

      // Add .visually-hidden class radio/checkbox.
      $element[$key]['#attributes']['class'][] = 'visually-hidden';

      // Add class to label attributes.
      $element[$key]['#label_attributes']['class'][] = 'webform-options-display-buttons-label';

      // Add #option_display to button.
      // @see \Drupal\webform_bootstrap_test_theme\Plugin\Preprocess\FormElement::preprocessElement
      $element[$key]['#option_display'] = 'button';

      // Add webform element property to trigger radio/checkbox template suggestions.
      // @see webform_theme_suggestions_form_element()
      $element[$key]['#webform_element'] = TRUE;
    }
  }

  // Issue #2839344: Some aria-describedby refers to not existing element ID.
  // @see https://www.drupal.org/project/drupal/issues/2839344
  if (!empty($element['#attributes']['aria-describedby'])) {
    foreach (Element::children($element) as $key) {
      if (empty($element[$key]['#attributes']['aria-describedby']) && $element['#attributes']['aria-describedby'] === $element[$key]['#attributes']['aria-describedby']) {
        unset($element[$key]['#attributes']['aria-describedby']);
      }
    }
  }
  return $element;
}