abstract class FieldItemBase

An entity field item.

Entity field items making use of this base class have to implement ComplexDataInterface::getPropertyDefinitions().

Hierarchy

Expanded class hierarchy of FieldItemBase

See also

\Drupal\Core\Entity\Field\FieldItemInterface

21 files declare their use of FieldItemBase
BooleanItem.php in drupal/core/lib/Drupal/Core/Entity/Field/Type/BooleanItem.php
Contains \Drupal\Core\Entity\Field\Type\BooleanItem.
DateItem.php in drupal/core/lib/Drupal/Core/Entity/Field/Type/DateItem.php
Contains \Drupal\Core\Entity\Field\Type\DateItem.
DateTimeItem.php in drupal/core/modules/datetime/lib/Drupal/datetime/Type/DateTimeItem.php
Contains Drupal\datetime\Type\DateTimeItem.
DecimalItem.php in drupal/core/modules/number/lib/Drupal/number/Type/DecimalItem.php
Contains \Drupal\number\Type\DecimalItem.
EmailItem.php in drupal/core/modules/email/lib/Drupal/email/Type/EmailItem.php
Contains \Drupal\email\Type\EmailItem.

... See full list

File

drupal/core/lib/Drupal/Core/Entity/Field/FieldItemBase.php, line 23
Contains \Drupal\Core\Entity\Field\FieldItemBase.

Namespace

Drupal\Core\Entity\Field
View source
abstract class FieldItemBase extends Map implements FieldItemInterface {

  /**
   * Overrides \Drupal\Core\TypedData\TypedData::__construct().
   */
  public function __construct(array $definition, $name = NULL, TypedDataInterface $parent = NULL) {
    parent::__construct($definition, $name, $parent);

    // Initialize computed properties by default, such that they get cloned
    // with the whole item.
    foreach ($this
      ->getPropertyDefinitions() as $name => $definition) {
      if (!empty($definition['computed'])) {
        $this->properties[$name] = \Drupal::typedData()
          ->getPropertyInstance($this, $name);
      }
    }
  }

  /**
   * Overrides \Drupal\Core\TypedData\TypedData::setValue().
   *
   * @param array|null $values
   *   An array of property values.
   */
  public function setValue($values, $notify = TRUE) {

    // Treat the values as property value of the first property, if no array is
    // given.
    if (isset($values) && !is_array($values)) {
      $keys = array_keys($this
        ->getPropertyDefinitions());
      $values = array(
        $keys[0] => $values,
      );
    }

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

    // Update any existing property objects.
    foreach ($this->properties as $name => $property) {
      $value = NULL;
      if (isset($values[$name])) {
        $value = $values[$name];
      }
      $property
        ->setValue($value, FALSE);
      unset($this->values[$name]);
    }
  }

  /**
   * Implements \Drupal\Core\Entity\Field\FieldItemInterface::__get().
   */
  public function __get($name) {

    // There is either a property object or a plain value - possibly for a
    // not-defined property. If we have a plain value, directly return it.
    if (isset($this->values[$name])) {
      return $this->values[$name];
    }
    elseif (isset($this->properties[$name])) {
      return $this->properties[$name]
        ->getValue();
    }
  }

  /**
   * Overrides \Drupal\Core\TypedData\Type\Map::set().
   */
  public function set($property_name, $value, $notify = TRUE) {

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

    // For defined properties there is either a property object or a plain
    // value that needs to be updated.
    if (isset($this->properties[$property_name])) {
      $this->properties[$property_name]
        ->setValue($value, FALSE);
      unset($this->values[$property_name]);
    }
    else {
      $this->values[$property_name] = $value;
    }
  }

  /**
   * Implements \Drupal\Core\Entity\Field\FieldItemInterface::__set().
   */
  public function __set($name, $value) {

    // Support setting values via property objects, but take care in as the
    // value of the 'entity' property is typed data also.
    if ($value instanceof TypedDataInterface && !$value instanceof EntityInterface) {
      $value = $value
        ->getValue();
    }
    $this
      ->set($name, $value);
  }

  /**
   * Implements \Drupal\Core\Entity\Field\FieldItemInterface::__isset().
   */
  public function __isset($name) {
    return isset($this->values[$name]) || isset($this->properties[$name]) && $this->properties[$name]
      ->getValue() !== NULL;
  }

  /**
   * Implements \Drupal\Core\Entity\Field\FieldItemInterface::__unset().
   */
  public function __unset($name) {
    $this
      ->set($name, NULL);
  }

  /**
   * Overrides \Drupal\Core\TypedData\Map::onChange().
   */
  public function onChange($property_name) {

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

    // Remove the plain value, such that any further __get() calls go via the
    // updated property object.
    unset($this->values[$property_name]);
  }

  /**
   * {@inheritdoc}
   */
  public function getConstraints() {
    $constraints = parent::getConstraints();

    // If property constraints are present add in a ComplexData constraint for
    // applying them.
    if (!empty($this->definition['property_constraints'])) {
      $constraints[] = \Drupal::typedData()
        ->getValidationConstraintManager()
        ->create('ComplexData', $this->definition['property_constraints']);
    }
    return $constraints;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldItemBase::getConstraints public function Implements \Drupal\Core\TypedData\TypedDataInterface::getConstraints(). Overrides TypedData::getConstraints
FieldItemBase::onChange public function Overrides \Drupal\Core\TypedData\Map::onChange(). Overrides Map::onChange
FieldItemBase::set public function Overrides \Drupal\Core\TypedData\Type\Map::set(). Overrides Map::set
FieldItemBase::setValue public function Overrides \Drupal\Core\TypedData\TypedData::setValue(). Overrides Map::setValue 5
FieldItemBase::__construct public function Overrides \Drupal\Core\TypedData\TypedData::__construct(). Overrides TypedData::__construct
FieldItemBase::__get public function Implements \Drupal\Core\Entity\Field\FieldItemInterface::__get(). Overrides FieldItemInterface::__get 1
FieldItemBase::__isset public function Implements \Drupal\Core\Entity\Field\FieldItemInterface::__isset(). Overrides FieldItemInterface::__isset 1
FieldItemBase::__set public function Implements \Drupal\Core\Entity\Field\FieldItemInterface::__set(). Overrides FieldItemInterface::__set
FieldItemBase::__unset public function Implements \Drupal\Core\Entity\Field\FieldItemInterface::__unset(). Overrides FieldItemInterface::__unset
Map::$properties protected property The array of properties, each implementing the TypedDataInterface.
Map::$values protected property An array of values for the contained properties.
Map::get public function Implements \Drupal\Core\TypedData\ComplexDataInterface::get(). Overrides ComplexDataInterface::get 1
Map::getIterator public function Implements \IteratorAggregate::getIterator().
Map::getProperties public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getProperties(). Overrides ComplexDataInterface::getProperties
Map::getPropertyDefinition public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinition(). Overrides ComplexDataInterface::getPropertyDefinition
Map::getPropertyDefinitions public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinitions(). Overrides ComplexDataInterface::getPropertyDefinitions 21
Map::getPropertyValues public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyValues(). Overrides ComplexDataInterface::getPropertyValues
Map::getString public function Overrides \Drupal\Core\TypedData\TypedData::getString(). Overrides TypedData::getString
Map::getValue public function Overrides \Drupal\Core\TypedData\TypedData::getValue(). Overrides TypedData::getValue
Map::isEmpty public function Implements \Drupal\Core\TypedData\ComplexDataInterface::isEmpty(). Overrides ComplexDataInterface::isEmpty 1
Map::setPropertyValues public function Implements \Drupal\Core\TypedData\ComplexDataInterface::setPropertyValues(). Overrides ComplexDataInterface::setPropertyValues
Map::__clone public function Magic method: Implements a deep clone.
TypedData::$definition protected property The data definition.
TypedData::$name protected property The property name.
TypedData::$parent protected property The parent typed data object.
TypedData::getDefinition public function Implements \Drupal\Core\TypedData\TypedDataInterface::getDefinition(). Overrides TypedDataInterface::getDefinition
TypedData::getName public function Implements \Drupal\Core\TypedData\TypedDataInterface::getName(). Overrides TypedDataInterface::getName
TypedData::getParent public function Implements \Drupal\Core\TypedData\TypedDataInterface::getParent(). Overrides TypedDataInterface::getParent
TypedData::getPropertyPath public function Implements \Drupal\Core\TypedData\TypedDataInterface::getPropertyPath(). Overrides TypedDataInterface::getPropertyPath
TypedData::getRoot public function Implements \Drupal\Core\TypedData\TypedDataInterface::getRoot(). Overrides TypedDataInterface::getRoot
TypedData::getType public function Implements \Drupal\Core\TypedData\TypedDataInterface::getType(). Overrides TypedDataInterface::getType
TypedData::setContext public function Implements \Drupal\Core\TypedData\TypedDataInterface::setContext(). Overrides TypedDataInterface::setContext 1
TypedData::validate public function Implements \Drupal\Core\TypedData\TypedDataInterface::validate(). Overrides TypedDataInterface::validate 3