You are here

public static function Tableselect::processTableselect in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Render/Element/Tableselect.php \Drupal\Core\Render\Element\Tableselect::processTableselect()

Creates checkbox or radio elements to populate a tableselect table.

Parameters

array $element: An associative array containing the properties and children of the tableselect element.

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

array $complete_form: The complete form structure.

Return value

array The processed element.

2 calls to Tableselect::processTableselect()
TableSelectTest::testProcessTableselectWithLinkTitle in core/tests/Drupal/Tests/Core/Render/Element/TableSelectTest.php
@covers ::processTableselect
TableSelectTest::testProcessTableselectWithStringTitle in core/tests/Drupal/Tests/Core/Render/Element/TableSelectTest.php
@covers ::processTableselect

File

core/lib/Drupal/Core/Render/Element/Tableselect.php, line 219

Class

Tableselect
Provides a form element for a table with radios or checkboxes in left column.

Namespace

Drupal\Core\Render\Element

Code

public static function processTableselect(&$element, FormStateInterface $form_state, &$complete_form) {
  if ($element['#multiple']) {
    $value = is_array($element['#value']) ? $element['#value'] : [];
  }
  else {

    // Advanced selection behavior makes no sense for radios.
    $element['#js_select'] = FALSE;
  }
  $element['#tree'] = TRUE;
  if (count($element['#options']) > 0) {
    if (!isset($element['#default_value']) || $element['#default_value'] === 0) {
      $element['#default_value'] = [];
    }

    // Create a checkbox or radio for each item in #options in such a way that
    // the value of the tableselect element behaves as if it had been of type
    // checkboxes or radios.
    foreach ($element['#options'] as $key => $choice) {

      // Do not overwrite manually created children.
      if (!isset($element[$key])) {
        if ($element['#multiple']) {
          $title = '';
          if (isset($element['#options'][$key]['title']) && is_array($element['#options'][$key]['title'])) {
            if (!empty($element['#options'][$key]['title']['data']['#title'])) {
              $title = new TranslatableMarkup('Update @title', [
                '@title' => $element['#options'][$key]['title']['data']['#title'],
              ]);
            }
          }
          $element[$key] = [
            '#type' => 'checkbox',
            '#title' => $title,
            '#title_display' => 'invisible',
            '#return_value' => $key,
            '#default_value' => isset($value[$key]) ? $key : NULL,
            '#attributes' => $element['#attributes'],
            '#ajax' => isset($element['#ajax']) ? $element['#ajax'] : NULL,
          ];
        }
        else {

          // 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' => '',
            '#return_value' => $key,
            '#default_value' => $element['#default_value'] == $key ? $key : NULL,
            '#attributes' => $element['#attributes'],
            '#parents' => $element['#parents'],
            '#id' => HtmlUtility::getUniqueId('edit-' . implode('-', $parents_for_id)),
            '#ajax' => isset($element['#ajax']) ? $element['#ajax'] : NULL,
          ];
        }
        if (isset($element['#options'][$key]['#weight'])) {
          $element[$key]['#weight'] = $element['#options'][$key]['#weight'];
        }
      }
    }
  }
  else {
    $element['#value'] = [];
  }
  return $element;
}