You are here

public function DbtngExampleAddForm::submitForm in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/dbtng_example/src/Form/DbtngExampleAddForm.php \Drupal\dbtng_example\Form\DbtngExampleAddForm::submitForm()

Form submission handler.

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

dbtng_example/src/Form/DbtngExampleAddForm.php, line 128

Class

DbtngExampleAddForm
Form to add a database entry, with all the interesting fields.

Namespace

Drupal\dbtng_example\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Gather the current user so the new record has ownership.
  $account = $this->currentUser;

  // Save the submitted entry.
  $entry = [
    'name' => $form_state
      ->getValue('name'),
    'surname' => $form_state
      ->getValue('surname'),
    'age' => $form_state
      ->getValue('age'),
    'uid' => $account
      ->id(),
  ];
  $return = $this->repository
    ->insert($entry);
  if ($return) {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Created entry @entry', [
      '@entry' => print_r($entry, TRUE),
    ]));
  }
}