2

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?

1 Answer 1

7

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.

8
  • Thank you, the #Tags =True is works, but the valueValidate(); didn't work, it giving me an error: 'There are no entities matching "asd".'
    – AnD
    Commented Jul 30, 2016 at 3:44
  • Name::valueValidate() was just to show how multiple entity references are handled. It was not to say "use that method."
    – avpaderno
    Commented Jul 30, 2016 at 3:46
  • Then is it possible to catch input that is not available in the list (database)?
    – AnD
    Commented Aug 1, 2016 at 12:15
  • Yes, it is possible.
    – avpaderno
    Commented Aug 1, 2016 at 12:22
  • shall i have a hint on how to do that? please...
    – AnD
    Commented Aug 1, 2016 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.