You are here

public static function Checkboxes::processCheckboxes in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Render/Element/Checkboxes.php \Drupal\Core\Render\Element\Checkboxes::processCheckboxes()
  2. 9 core/lib/Drupal/Core/Render/Element/Checkboxes.php \Drupal\Core\Render\Element\Checkboxes::processCheckboxes()

Processes a checkboxes form element.

1 call to Checkboxes::processCheckboxes()
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/Checkboxes.php, line 61

Class

Checkboxes
Provides a form element for a set of checkboxes.

Namespace

Drupal\Core\Render\Element

Code

public static function processCheckboxes(&$element, FormStateInterface $form_state, &$complete_form) {
  $value = is_array($element['#value']) ? $element['#value'] : [];
  $element['#tree'] = TRUE;
  if (count($element['#options']) > 0) {
    if (!isset($element['#default_value']) || $element['#default_value'] == 0) {
      $element['#default_value'] = [];
    }
    $weight = 0;
    foreach ($element['#options'] as $key => $choice) {

      // Integer 0 is not a valid #return_value, so use '0' instead.
      // @see \Drupal\Core\Render\Element\Checkbox::valueCallback().
      // @todo For Drupal 8, cast all integer keys to strings for consistency
      //   with \Drupal\Core\Render\Element\Radios::processRadios().
      if ($key === 0) {
        $key = '0';
      }

      // 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;

      // Only enabled checkboxes receive their values from the form
      // submission, the disabled checkboxes use their default value.
      $default_value = NULL;
      if (isset($value[$key]) || !empty($element[$key]['#disabled']) && in_array($key, $element['#default_value'], TRUE)) {
        $default_value = $key;
      }
      $element += [
        $key => [],
      ];
      $element[$key] += [
        '#type' => 'checkbox',
        '#title' => $choice,
        '#return_value' => $key,
        '#default_value' => $default_value,
        '#attributes' => $element['#attributes'],
        '#ajax' => $element['#ajax'] ?? NULL,
        // Errors should only be shown on the parent checkboxes element.
        '#error_no_message' => TRUE,
        '#weight' => $weight,
      ];
    }
  }
  return $element;
}