You are here

public function DbtngExampleUpdateForm::updateCallback in Examples for Developers 8

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

AJAX callback handler for the pid select.

When the pid changes, populates the defaults from the database in the form.

File

dbtng_example/src/Form/DbtngExampleUpdateForm.php, line 136

Class

DbtngExampleUpdateForm
Sample UI to update a record.

Namespace

Drupal\dbtng_example\Form

Code

public function updateCallback(array $form, FormStateInterface $form_state) {

  // Gather the DB results from $form_state.
  $entries = $form_state
    ->getValue('entries');

  // Use the specific entry for this $form_state.
  $entry = $entries[$form_state
    ->getValue('pid')];

  // Setting the #value of items is the only way I was able to figure out
  // to get replaced defaults on these items. #default_value will not do it
  // and shouldn't.
  foreach ([
    'name',
    'surname',
    'age',
  ] as $item) {
    $form[$item]['#value'] = $entry->{$item};
  }
  return $form;
}