function EntityAutocomplete::submitForm

Same name in other branches
  1. 4.0.x modules/ajax_example/src/Form/EntityAutocomplete.php \Drupal\ajax_example\Form\EntityAutocomplete::submitForm()

On submit, show the user the names of the users they selected.

Overrides FormInterface::submitForm

File

modules/ajax_example/src/Form/EntityAutocomplete.php, line 117

Class

EntityAutocomplete
A simple autocomplete form which looks up usernames.

Namespace

Drupal\ajax_example\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
    $state_users = $form_state->getValue('users');
    $users = [];
    foreach ($state_users as $state_user) {
        $uid = $state_user['target_id'];
        $users[] = $this->entityTypeManager
            ->getStorage('user')
            ->load($uid)
            ->getDisplayName();
    }
    $this->messenger()
        ->addMessage('These are your users: ' . implode(' ', $users));
}