How do I get multiple references from the autocomplete?

I am using the following form element.

$form['group']['one'] = array(     '#type' => 'entity_autocomplete',     '#title' => t('Tags'),     '#target_type' => 'user', ); 

I submit values similar to the following ones.

user00 (0), user01 (1), user02 (2), user0x (3)

What I receive from the form is the following:

one = 3

It only picks up the last ID.

How can I get all the references entered in the form element? How does the form element handle the references to not existing entities?

Replay

The comma is used to separate multiple references, but looking at the code in Name::valueForm() I would say the form element needs to use a property more.

  $form['value'] = array(
    '#type' => 'entity_autocomplete',
    '#title' => $this->t('Usernames'),
    '#description' => $this->t('Enter a comma separated list of user names.'),
    '#target_type' => 'user',
    '#tags' => TRUE,
    '#default_value' => $default_value,
    '#process_default_value' => $this->isExposed(),
  );

Settings #tags to TRUE makes the the form element accept multiple values.

The format of the values assigned to the form element is evident in EntityAutocomplete::validateEntityAutocomplete() (its validation handler), which uses this code.

  $input_values = $element['#tags'] ? Tags::explode($element['#value']) : array($element['#value']);

  foreach ($input_values as $input) {
    $match = static::extractEntityIdFromAutocompleteInput($input);
    if ($match === NULL) {
      // Try to get a match from the input string when the user didn't use
      // the autocomplete but filled in a value manually.
      $match = static::matchEntityByTitle($handler, $input, $element, $form_state, !$autocreate);
    }

    if ($match !== NULL) {
      $value[] = array(
        'target_id' => $match,
      );
    }
    elseif ($autocreate) {
      /** @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionWithAutocreateInterface $handler */
      // Auto-create item. See an example of how this is handled in
      // \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::presave().
      $value[] = array(
        'entity' => $handler->createNewEntity($element['#target_type'], $element['#autocreate']['bundle'], $input, $element['#autocreate']['uid']),
      );
    }

The form element value is an array of item whose target_id key is an object of the requested entity.

(From that code, it is also clear that #tags makes the form element accepts multiple references separated by commas.)

As for the using reference that aren't found, you can decide what the autocomplete does by setting #validate_reference: If you set it to TRUE, and #autocreate is not set to TRUE, the autocomplete will accept only existing references.

Category: 8 Time: 2016-07-29 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.433 (s). 12 q(s)