function EntityAutocomplete::buildForm

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

Overrides FormInterface::buildForm

File

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

Class

EntityAutocomplete
A simple autocomplete form which looks up usernames.

Namespace

Drupal\ajax_example\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
    $form['info'] = [
        '#markup' => '<div>' . $this->t("This example uses the <code>entity_autocomplete</code> form element to select users. You'll need a few users on your system for it to make sense.") . '</div>',
    ];
    // Here we use the delightful entity_autocomplete form element. It allows us
    // to consistently select entities. See https://www.drupal.org/node/2418529.
    $form['users'] = [
        // A type of entity_autocomplete lets Drupal know it should autocomplete
        // entities.
'#type' => 'entity_autocomplete',
        // We can specify entity types to autocomplete.
'#target_type' => 'user',
        // Specifying #tags as TRUE allows for multiple selections, separated by
        // commas.
'#tags' => TRUE,
        '#title' => $this->t('Choose a user (Separate with commas)'),
    ];
    $form['actions'] = [
        '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
        '#type' => 'submit',
        '#value' => $this->t('Submit'),
    ];
    return $form;
}