Score:-1

Base field defined as entity reference with cardinality unlimited, only allowing one entry

fi flag

I have defined a custom entity type "Event" and I want a field on it called "Attendees" which should allow the user to enter multiple attendees.

I have defined the field thus:

   $fields['attendees'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Attendees'))
  ->setDescription(t('People attending this event.'))
  ->setSetting('target_type', 'oc_actor')
  ->setSetting('handler', 'default')
  ->setTranslatable(TRUE)
  ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
  ->setDisplayOptions('view', [
    'type' => 'string',
    'weight' => 50,
  ])
  ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete_tags',
    'weight' => 50,
  ])
  ->setRequired(FALSE);

However, the resulting field does not have unlimited cardinality. It only allows the user to enter one attendee. Compare and contrast with the "Test" field that I added beneath it using the GUI:

enter image description here

I have done a fresh install of the whole site since I last touched this code so it is not a matter of needing to add an update hook or clear the cache or anything like that.

Any ideas?

Score:2
ph flag

You have it set to entity_reference_autocomplete_tags which means the multiple values end up comma separated. You probably want just entity_reference_autocomplete.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.