You are here

public static function Radios::processRadios in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Render/Element/Radios.php \Drupal\Core\Render\Element\Radios::processRadios()

Expands a radios element into individual radio elements.

1 call to Radios::processRadios()
template_preprocess_views_ui_build_group_filter_form in core/modules/views_ui/views_ui.theme.inc
Prepares variables for Views UI build group filter form templates.

File

core/lib/Drupal/Core/Render/Element/Radios.php, line 55

Class

Radios
Provides a form element for a set of radio buttons.

Namespace

Drupal\Core\Render\Element

Code

public static function processRadios(&$element, FormStateInterface $form_state, &$complete_form) {
  if (count($element['#options']) > 0) {
    $weight = 0;
    foreach ($element['#options'] as $key => $choice) {

      // Maintain order of options as defined in #options, in case the element
      // defines custom option sub-elements, but does not define all option
      // sub-elements.
      $weight += 0.001;
      $element += [
        $key => [],
      ];

      // Generate the parents as the autogenerator does, so we will have a
      // unique id for each radio button.
      $parents_for_id = array_merge($element['#parents'], [
        $key,
      ]);
      $element[$key] += [
        '#type' => 'radio',
        '#title' => $choice,
        // The key is sanitized in Drupal\Core\Template\Attribute during output
        // from the theme function.
        '#return_value' => $key,
        // Use default or FALSE. A value of FALSE means that the radio button is
        // not 'checked'.
        '#default_value' => isset($element['#default_value']) ? $element['#default_value'] : FALSE,
        '#attributes' => $element['#attributes'],
        '#parents' => $element['#parents'],
        '#id' => HtmlUtility::getUniqueId('edit-' . implode('-', $parents_for_id)),
        '#ajax' => isset($element['#ajax']) ? $element['#ajax'] : NULL,
        // Errors should only be shown on the parent radios element.
        '#error_no_message' => TRUE,
        '#weight' => $weight,
      ];
    }
  }
  return $element;
}