function TableDragExampleRootLeafForm::submitForm

Submit handler for the form.

Updates the 'weight' column for each element in our table, taking into account that item's new order after the drag and drop actions have been performed.

Parameters

array $form: Render array representing from.

\Drupal\Core\Form\FormStateInterface $form_state: Current form state.

File

modules/tabledrag_example/src/Form/TableDragExampleRootLeafForm.php, line 253

Class

TableDragExampleRootLeafForm
Example form with a table with draggable items.

Namespace

Drupal\tabledrag_example\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  // Because the form elements were keyed with the item ids from the database,
  // we can simply iterate through the submitted values.
  $submissions = $form_state->getValue('table-row');
  foreach ($submissions as $id => $item) {
    $this->database
      ->update('tabledrag_example')
      ->fields([
      'weight' => $item['weight'],
      'pid' => $item['pid'],
      'description' => $item['description'],
    ])
      ->condition('id', $id, '=')
      ->execute();
  }
}