You are here

public static function YamlFormTableSort::validateYamlFormTableSelectOrder in YAML Form 8

Validates yamlform_table_other.

File

src/Element/YamlFormTableSort.php, line 230

Class

YamlFormTableSort
Provides a form element for a sortable table 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]['value']['#value']) {
      $checked_values[] = [
        'value' => $element[$key]['value']['#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);
}