You are here

class EntityAutocomplete in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/ajax_example/src/Form/EntityAutocomplete.php \Drupal\ajax_example\Form\EntityAutocomplete

A simple autocomplete form which looks up usernames.

Hierarchy

Expanded class hierarchy of EntityAutocomplete

Related topics

1 string reference to 'EntityAutocomplete'
ajax_example.routing.yml in ajax_example/ajax_example.routing.yml
ajax_example/ajax_example.routing.yml

File

ajax_example/src/Form/EntityAutocomplete.php, line 18

Namespace

Drupal\ajax_example\Form
View source
class EntityAutocomplete implements FormInterface, ContainerInjectionInterface {
  use StringTranslationTrait;
  use MessengerTrait;

  /**
   * The entity type manager service.
   *
   * We need this for the submit handler.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Container injection factory.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   The service discovery container.
   *
   * @return self
   *   The form object.
   */
  public static function create(ContainerInterface $container) {
    $form = new static($container
      ->get('entity_type.manager'));
    $form
      ->setStringTranslation($container
      ->get('string_translation'));
    $form
      ->setMessenger($container
      ->get('messenger'));
    return $form;
  }

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'ajax_example_autocomplete_user';
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   *
   * Here we validate and signal an error if there are no users selected.
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $state_users = $form_state
      ->getValue('users');
    if (empty($state_users)) {
      $form_state
        ->setErrorByName('users', 'There were no users selected.');
    }
  }

  /**
   * {@inheritdoc}
   *
   * On submit, show the user the names of the users they selected.
   */
  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));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityAutocomplete::$entityTypeManager protected property The entity type manager service.
EntityAutocomplete::buildForm public function Form constructor. Overrides FormInterface::buildForm
EntityAutocomplete::create public static function Container injection factory. Overrides ContainerInjectionInterface::create
EntityAutocomplete::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
EntityAutocomplete::submitForm public function On submit, show the user the names of the users they selected. Overrides FormInterface::submitForm
EntityAutocomplete::validateForm public function Here we validate and signal an error if there are no users selected. Overrides FormInterface::validateForm
EntityAutocomplete::__construct public function Constructor.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.