EntityReferenceFieldItemList::$entity

The first field item's referenced entity, if it exists.

Type: entity

File

core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php, line 19

Class

EntityReferenceFieldItemList
Defines an item list class for entity reference fields.

Namespace

Drupal\Core\Field

Code

public ?EntityInterface $entity {
  get => $this->first()?->__get('entity');
  set(?EntityInterface $entity) {
    $item = $this->first() ?: $this->appendItem();
    $item->set('entity', $entity);
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.