function DbtngExampleUpdateForm::updateCallback

Same name and namespace in other branches
  1. 4.0.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

modules/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;
}