You are here

public static function WebformTableSelectSort::validateWebformTableSelectOrder in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Element/WebformTableSelectSort.php \Drupal\webform\Element\WebformTableSelectSort::validateWebformTableSelectOrder()

Validates webform_tableselect_other.

File

src/Element/WebformTableSelectSort.php, line 256

Class

WebformTableSelectSort
Provides a webform element for a sortable tableselect element.

Namespace

Drupal\webform\Element

Code

public static function validateWebformTableSelectOrder(&$element, FormStateInterface $form_state, &$complete_form) {

  // Get and sort checked values.
  $checked_values = [];
  foreach (Element::children($element) as $key) {
    if ($element[$key]['checkbox']['#value']) {
      $checked_values[] = [
        'value' => $element[$key]['checkbox']['#value'],
        'weight' => $element[$key]['weight']['#value'],
      ];
    }
  }
  uasort($checked_values, [
    'Drupal\\Component\\Utility\\SortArray',
    'sortByWeightElement',
  ]);

  // Set values.
  $values = [];
  foreach ($checked_values as $item) {
    $values[$item['value']] = $item['value'];
  }

  // Clear the element's value by setting it to NULL.
  $form_state
    ->setValueForElement($element, NULL);

  // Now, set the values as the element's value.
  $element['#value'] = $values;
  $form_state
    ->setValueForElement($element, $values);
}