You are here

public function TableDragExampleRootLeafForm::submitForm in Examples for Developers 3.x

Same name and namespace in other branches
  1. 8 tabledrag_example/src/Form/TableDragExampleRootLeafForm.php \Drupal\tabledrag_example\Form\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.

Overrides FormInterface::submitForm

File

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

Class

TableDragExampleRootLeafForm
Table drag example root leaf form.

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();
  }
}