You are here

public static function YamlFormTableSelectSort::validateYamlFormTableSelectOrder in YAML Form 8

Validates yamlform_tableselect_other.

File

src/Element/YamlFormTableSelectSort.php, line 258

Class

YamlFormTableSelectSort
Provides a form element for a sortable tableselect element.

Namespace

Drupal\yamlform\Element

Code

public static function validateYamlFormTableSelectOrder(&$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.
  $form_state
    ->setValueForElement($element, $values);
}