class EntityReferenceItem

Defines the 'entityreference_field' entity field item.

Required settings (below the definition's 'settings' key) are:

  • entity type: The entity type to reference.

Hierarchy

Expanded class hierarchy of EntityReferenceItem

1 file declares its use of EntityReferenceItem

File

drupal/core/lib/Drupal/Core/Entity/Field/Type/EntityReferenceItem.php, line 19
Definition of Drupal\Core\Entity\Field\Type\EntityReferenceItem.

Namespace

Drupal\Core\Entity\Field\Type
View source
class EntityReferenceItem extends FieldItemBase {

  /**
   * Field definitions of the contained properties.
   *
   * @see self::getPropertyDefinitions()
   *
   * @var array
   */
  static $propertyDefinitions;

  /**
   * Implements ComplexDataInterface::getPropertyDefinitions().
   */
  public function getPropertyDefinitions() {

    // Definitions vary by entity type, so key them by entity type.
    $entity_type = $this->definition['settings']['entity type'];
    if (!isset(self::$propertyDefinitions[$entity_type])) {
      self::$propertyDefinitions[$entity_type]['value'] = array(
        // @todo: Lookup the entity type's ID data type and use it here.
        'type' => 'integer',
        'label' => t('Entity ID'),
      );
      self::$propertyDefinitions[$entity_type]['entity'] = array(
        'type' => 'entity',
        'constraints' => array(
          'entity type' => $entity_type,
        ),
        'label' => t('Entity'),
        'description' => t('The referenced entity'),
        // The entity object is computed out of the entity id.
        'computed' => TRUE,
        'read-only' => FALSE,
        'settings' => array(
          'id source' => 'value',
        ),
      );
    }
    return self::$propertyDefinitions[$entity_type];
  }

  /**
   * Overrides FieldItemBase::setValue().
   */
  public function setValue($values) {

    // Treat the values as property value of the entity field, if no array
    // is given.
    if (!is_array($values)) {
      $values = array(
        'entity' => $values,
      );
    }

    // Entity is computed out of the ID, so we only need to update the ID. Only
    // set the entity field if no ID is given.
    if (isset($values['value'])) {
      $this->properties['value']
        ->setValue($values['value']);
    }
    else {
      $this->properties['entity']
        ->setValue(isset($values['entity']) ? $values['entity'] : NULL);
    }
    unset($values['entity'], $values['value']);
    if ($values) {
      throw new InvalidArgumentException('Property ' . key($values) . ' is unknown.');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityReferenceItem::$propertyDefinitions static property Field definitions of the contained properties.
EntityReferenceItem::getPropertyDefinitions public function Implements ComplexDataInterface::getPropertyDefinitions(). Overrides ComplexDataInterface::getPropertyDefinitions
EntityReferenceItem::setValue public function Overrides FieldItemBase::setValue(). Overrides FieldItemBase::setValue
FieldItemBase::$name protected property The item delta or name.
FieldItemBase::$parent protected property The parent entity field.
FieldItemBase::$properties protected property The array of properties.
FieldItemBase::get public function Implements ComplexDataInterface::get(). Overrides ComplexDataInterface::get
FieldItemBase::getIterator public function Implements IteratorAggregate::getIterator().
FieldItemBase::getName public function Implements ContextAwareInterface::getName(). Overrides ContextAwareInterface::getName
FieldItemBase::getParent public function Implements ContextAwareInterface::getParent(). Overrides ContextAwareInterface::getParent
FieldItemBase::getProperties public function Implements ComplexDataInterface::getProperties(). Overrides ComplexDataInterface::getProperties
FieldItemBase::getPropertyDefinition public function Implements ComplexDataInterface::getPropertyDefinition(). Overrides ComplexDataInterface::getPropertyDefinition
FieldItemBase::getPropertyValues public function Implements ComplexDataInterface::getPropertyValues(). Overrides ComplexDataInterface::getPropertyValues
FieldItemBase::getString public function Implements TypedDataInterface::getString(). Overrides TypedData::getString
FieldItemBase::getValue public function Implements TypedDataInterface::getValue(). Overrides TypedData::getValue
FieldItemBase::isEmpty public function Implements ComplexDataInterface::isEmpty(). Overrides ComplexDataInterface::isEmpty
FieldItemBase::set public function Implements ComplexDataInterface::set(). Overrides ComplexDataInterface::set
FieldItemBase::setName public function Implements ContextAwareInterface::setName(). Overrides ContextAwareInterface::setName
FieldItemBase::setParent public function Implements ContextAwareInterface::setParent(). Overrides ContextAwareInterface::setParent
FieldItemBase::setPropertyValues public function Implements ComplexDataInterface::setPropertyValues(). Overrides ComplexDataInterface::setPropertyValues
FieldItemBase::validate public function Implements TypedDataInterface::validate(). Overrides TypedDataInterface::validate
FieldItemBase::__clone public function Implements a deep clone.
FieldItemBase::__construct public function Implements TypedDataInterface::__construct(). Overrides TypedData::__construct
FieldItemBase::__get public function Implements FieldItemInterface::__get(). Overrides FieldItemInterface::__get
FieldItemBase::__isset public function Implements FieldItemInterface::__isset(). Overrides FieldItemInterface::__isset
FieldItemBase::__set public function Implements FieldItemInterface::__set(). Overrides FieldItemInterface::__set
FieldItemBase::__unset public function Implements FieldItemInterface::__unset(). Overrides FieldItemInterface::__unset
TypedData::$definition protected property The data definition.
TypedData::getDefinition public function Implements TypedDataInterface::getDefinition(). Overrides TypedDataInterface::getDefinition
TypedData::getType public function Implements TypedDataInterface::getType(). Overrides TypedDataInterface::getType