You are here

public function TableDragExampleNestedForm::submitForm in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/tabledrag_example/src/Form/TableDragExampleNestedForm.php \Drupal\tabledrag_example\Form\TableDragExampleNestedForm::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: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

tabledrag_example/src/Form/TableDragExampleNestedForm.php, line 219

Class

TableDragExampleNestedForm
Table drag example nested 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();
  }
}