You are here

class EntityReference in Drupal 10

Same name in this branch
  1. 10 core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php \Drupal\Core\Entity\Plugin\DataType\EntityReference
  2. 10 core/modules/views/src/Plugin/views/style/EntityReference.php \Drupal\views\Plugin\views\style\EntityReference
  3. 10 core/modules/views/src/Plugin/views/row/EntityReference.php \Drupal\views\Plugin\views\row\EntityReference
  4. 10 core/modules/field/src/Plugin/migrate/field/d7/EntityReference.php \Drupal\field\Plugin\migrate\field\d7\EntityReference
Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php \Drupal\Core\Entity\Plugin\DataType\EntityReference
  2. 9 core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php \Drupal\Core\Entity\Plugin\DataType\EntityReference

Defines an 'entity_reference' data type.

This serves as 'entity' property of entity reference field items and gets its value set from the parent, i.e. LanguageItem.

The plain value of this reference is the entity object, i.e. an instance of \Drupal\Core\Entity\EntityInterface. For setting the value the entity object or the entity ID may be passed.

Note that the definition of the referenced entity's type is required, whereas defining referenceable entity bundle(s) is optional. A reference defining the type and bundle of the referenced entity can be created as following:

$definition = \Drupal\Core\Entity\EntityDefinition::create($entity_type)
  ->addConstraint('Bundle', $bundle);
\Drupal\Core\TypedData\DataReferenceDefinition::create('entity')
  ->setTargetDefinition($definition);

Plugin annotation


@DataType(
  id = "entity_reference",
  label = @Translation("Entity reference"),
  definition_class = "\Drupal\Core\TypedData\DataReferenceDefinition"
)

Hierarchy

  • class \Drupal\Core\Entity\Plugin\DataType\EntityReference extends \Drupal\Core\TypedData\DataReferenceBase

Expanded class hierarchy of EntityReference

1 file declares its use of EntityReference
ContentEntityBase.php in core/lib/Drupal/Core/Entity/ContentEntityBase.php
2 string references to 'EntityReference'
views.view.test_entity_reference.yml in core/modules/system/tests/modules/entity_reference_test/config/install/views.view.test_entity_reference.yml
core/modules/system/tests/modules/entity_reference_test/config/install/views.view.test_entity_reference.yml
views.view.test_entity_reference_entity_test.yml in core/modules/system/tests/modules/entity_reference_test/config/install/views.view.test_entity_reference_entity_test.yml
core/modules/system/tests/modules/entity_reference_test/config/install/views.view.test_entity_reference_entity_test.yml

File

core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php, line 34

Namespace

Drupal\Core\Entity\Plugin\DataType
View source
class EntityReference extends DataReferenceBase {

  /**
   * The entity ID.
   *
   * @var int|string
   */
  protected $id;

  /**
   * Gets the definition of the referenced entity.
   *
   * @return \Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface
   *   The reference target's definition.
   */
  public function getTargetDefinition() {
    return $this->definition
      ->getTargetDefinition();
  }

  /**
   * Checks whether the target entity has not been saved yet.
   *
   * @return bool
   *   TRUE if the entity is new, FALSE otherwise.
   */
  public function isTargetNew() {

    // If only an ID is given, the reference cannot be a new entity.
    return !isset($this->id) && isset($this->target) && $this->target
      ->getValue()
      ->isNew();
  }

  /**
   * {@inheritdoc}
   */
  public function getTarget() {
    if (!isset($this->target) && isset($this->id)) {

      // If we have a valid reference, return the entity's TypedData adapter.
      $entity = \Drupal::entityTypeManager()
        ->getStorage($this
        ->getTargetDefinition()
        ->getEntityTypeId())
        ->load($this->id);
      $this->target = isset($entity) ? $entity
        ->getTypedData() : NULL;
    }
    return $this->target;
  }

  /**
   * {@inheritdoc}
   */
  public function getTargetIdentifier() {
    if (isset($this->id)) {
      return $this->id;
    }
    elseif ($entity = $this
      ->getValue()) {
      return $entity
        ->id();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setValue($value, $notify = TRUE) {
    unset($this->target);
    unset($this->id);

    // Both the entity ID and the entity object may be passed as value. The
    // reference may also be unset by passing NULL as value.
    if (!isset($value)) {
      $this->target = NULL;
    }
    elseif ($value instanceof EntityInterface) {
      $this->target = $value
        ->getTypedData();
    }
    elseif (!is_scalar($value) || $this
      ->getTargetDefinition()
      ->getEntityTypeId() === NULL) {
      throw new \InvalidArgumentException('Value is not a valid entity.');
    }
    else {
      $this->id = $value;
    }

    // Notify the parent of any changes.
    if ($notify && isset($this->parent)) {
      $this->parent
        ->onChange($this->name);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getString() {
    if ($entity = $this
      ->getValue()) {
      return $entity
        ->label();
    }
    return '';
  }

}

Members