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.