Custom entity types with multiple value properties I'm creating a custom entity type, that has the following base field (defined in a class that extends ContentEntityBase)

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields['recipient_id'] = BaseFieldDefinition::create('entity_reference')
    -&gt;setLabel(t('Recipient(s)'))
    -&gt;setDescription(t('The recipient(s)'))
    -&gt;setSetting('target_type', 'user')
    -&gt;setSetting('handler', 'default')
    -&gt;setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
    -&gt;setDisplayOptions('view', array(
      'label' =&gt; 'above',
      'type' =&gt; 'entity_reference_label',
      'weight' =&gt; -3,
    ))
    -&gt;setDisplayOptions('form', array(
      'type' =&gt; 'entity_reference_autocomplete',
      'settings' =&gt; array(
        'match_operator' =&gt; 'CONTAINS',
        'size' =&gt; 60,
        'autocomplete_type' =&gt; 'tags',
        'placeholder' =&gt; '',
      ),
      'weight' =&gt; -3,
    ))
    -&gt;setDisplayConfigurable('form', TRUE)
    -&gt;setDisplayConfigurable('view', TRUE);
}


I've set the field to have unlimited cardinality. I'm trying to create the getters and setters for this field, however I'm not finding any examples of entity types with multiple values.

I need to create the following getters and setters:

// Add a single recipient to the entity
public function setRecipient()
// retrieve all recipients for the entity
public function getRecipients()


Does anyone have an example of how to set up these functions to work with a multiple value field?
// Add a single recipient to the entity
public function setRecipient()


What now, add or set? If it is adding to the existing list, I'd recommend to name it addRecipient().

I'd also recommend to name the field recipients or so, to indicate that it has multiple values.

You can add an item to a field item list with \Drupal\Core\TypedData\ListInterface::appendItem(), for example $this->get('recipient_id')->appendItem($recipient_entity_or_id_or_value_array)

To get all referenced entities, there is a referencedEntities() method on entity reference field item lists, or you can use a loop.