An entity field item.
Entity field items making use of this base class have to implement ComplexDataInterface::getPropertyDefinitions().
Expanded class hierarchy of FieldItemBase
\Drupal\Core\Entity\Field\FieldItemInterface
abstract class FieldItemBase extends TypedData implements IteratorAggregate, FieldItemInterface {
/**
* The item delta or name.
*
* @var integer
*/
protected $name;
/**
* The parent entity field.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
protected $parent;
/**
* The array of properties.
*
* Field objects are instantiated during object construction and cannot be
* replaced by others, so computed properties can safely store references on
* other properties.
*
* @var array<TypedDataInterface>
*/
protected $properties = array();
/**
* Implements TypedDataInterface::__construct().
*/
public function __construct(array $definition) {
$this->definition = $definition;
// Initialize all property objects, but postpone the creating of computed
// properties to a second step. That way computed properties can safely get
// references on non-computed properties during construction.
$step2 = array();
foreach ($this
->getPropertyDefinitions() as $name => $definition) {
if (empty($definition['computed'])) {
$context = array(
'name' => $name,
'parent' => $this,
);
$this->properties[$name] = typed_data()
->create($definition, NULL, $context);
}
else {
$step2[$name] = $definition;
}
}
foreach ($step2 as $name => $definition) {
$context = array(
'name' => $name,
'parent' => $this,
);
$this->properties[$name] = typed_data()
->create($definition, NULL, $context);
}
}
/**
* Implements TypedDataInterface::getValue().
*/
public function getValue() {
$values = array();
foreach ($this
->getProperties() as $name => $property) {
$values[$name] = $property
->getValue();
}
return $values;
}
/**
* Implements TypedDataInterface::setValue().
*
* @param array $values
* An array of property values.
*/
public function setValue($values) {
// Treat the values as property value of the first property, if no array is
// given and we only have one property.
if (!is_array($values) && count($this->properties) == 1) {
$keys = array_keys($this->properties);
$values = array(
$keys[0] => $values,
);
}
foreach ($this->properties as $name => $property) {
$property
->setValue(isset($values[$name]) ? $values[$name] : NULL);
}
// @todo: Throw an exception for invalid values once conversion is
// totally completed.
}
/**
* Implements TypedDataInterface::getString().
*/
public function getString() {
$strings = array();
foreach ($this
->getProperties() as $property) {
$strings[] = $property
->getString();
}
return implode(', ', array_filter($strings));
}
/**
* Implements TypedDataInterface::validate().
*/
public function validate() {
// @todo implement
}
/**
* Implements ComplexDataInterface::get().
*/
public function get($property_name) {
if (!isset($this->properties[$property_name])) {
throw new InvalidArgumentException('Field ' . check_plain($property_name) . ' is unknown.');
}
return $this->properties[$property_name];
}
/**
* Implements ComplexDataInterface::set().
*/
public function set($property_name, $value) {
$this
->get($property_name)
->setValue($value);
}
/**
* Implements FieldItemInterface::__get().
*/
public function __get($name) {
return $this
->get($name)
->getValue();
}
/**
* Implements FieldItemInterface::__set().
*/
public function __set($name, $value) {
// Support setting values via property objects.
if ($value instanceof TypedDataInterface) {
$value = $value
->getValue();
}
$this
->get($name)
->setValue($value);
}
/**
* Implements FieldItemInterface::__isset().
*/
public function __isset($name) {
return isset($this->properties[$name]) && $this->properties[$name]
->getValue() !== NULL;
}
/**
* Implements FieldItemInterface::__unset().
*/
public function __unset($name) {
if (isset($this->properties[$name])) {
$this->properties[$name]
->setValue(NULL);
}
}
/**
* Implements ContextAwareInterface::getName().
*/
public function getName() {
return $this->name;
}
/**
* Implements ContextAwareInterface::setName().
*/
public function setName($name) {
$this->name = $name;
}
/**
* Implements ContextAwareInterface::getParent().
*
* @return \Drupal\Core\Entity\Field\FieldInterface
*/
public function getParent() {
return $this->parent;
}
/**
* Implements ContextAwareInterface::setParent().
*/
public function setParent($parent) {
$this->parent = $parent;
}
/**
* Implements ComplexDataInterface::getProperties().
*/
public function getProperties($include_computed = FALSE) {
$properties = array();
foreach ($this
->getPropertyDefinitions() as $name => $definition) {
if ($include_computed || empty($definition['computed'])) {
$properties[$name] = $this->properties[$name];
}
}
return $properties;
}
/**
* Implements ComplexDataInterface::getPropertyValues().
*/
public function getPropertyValues() {
return $this
->getValue();
}
/**
* Implements ComplexDataInterface::setPropertyValues().
*/
public function setPropertyValues($values) {
foreach ($values as $name => $value) {
$this
->get($name)
->setValue($value);
}
}
/**
* Implements IteratorAggregate::getIterator().
*/
public function getIterator() {
return new ArrayIterator($this
->getProperties());
}
/**
* Implements ComplexDataInterface::getPropertyDefinition().
*/
public function getPropertyDefinition($name) {
$definitions = $this
->getPropertyDefinitions();
return isset($definitions[$name]) ? $definitions[$name] : FALSE;
}
/**
* Implements ComplexDataInterface::isEmpty().
*/
public function isEmpty() {
foreach ($this
->getProperties() as $property) {
if ($property
->getValue() !== NULL) {
return FALSE;
}
}
return TRUE;
}
/**
* Implements a deep clone.
*/
public function __clone() {
foreach ($this->properties as $name => $property) {
$this->properties[$name] = clone $property;
if ($property instanceof ContextAwareInterface) {
$this->properties[$name]
->setParent($this);
}
}
}
}
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ComplexDataInterface:: |
public | function | Gets an array property definitions of contained properties. | 12 |
FieldItemBase:: |
protected | property | The item delta or name. | |
FieldItemBase:: |
protected | property | The parent entity field. | |
FieldItemBase:: |
protected | property | The array of properties. | |
FieldItemBase:: |
public | function |
Implements ComplexDataInterface::get(). Overrides ComplexDataInterface:: |
|
FieldItemBase:: |
public | function | Implements IteratorAggregate::getIterator(). | |
FieldItemBase:: |
public | function |
Implements ContextAwareInterface::getName(). Overrides ContextAwareInterface:: |
|
FieldItemBase:: |
public | function |
Implements ContextAwareInterface::getParent(). Overrides ContextAwareInterface:: |
|
FieldItemBase:: |
public | function |
Implements ComplexDataInterface::getProperties(). Overrides ComplexDataInterface:: |
|
FieldItemBase:: |
public | function |
Implements ComplexDataInterface::getPropertyDefinition(). Overrides ComplexDataInterface:: |
|
FieldItemBase:: |
public | function |
Implements ComplexDataInterface::getPropertyValues(). Overrides ComplexDataInterface:: |
|
FieldItemBase:: |
public | function |
Implements TypedDataInterface::getString(). Overrides TypedData:: |
|
FieldItemBase:: |
public | function |
Implements TypedDataInterface::getValue(). Overrides TypedData:: |
|
FieldItemBase:: |
public | function |
Implements ComplexDataInterface::isEmpty(). Overrides ComplexDataInterface:: |
|
FieldItemBase:: |
public | function |
Implements ComplexDataInterface::set(). Overrides ComplexDataInterface:: |
|
FieldItemBase:: |
public | function |
Implements ContextAwareInterface::setName(). Overrides ContextAwareInterface:: |
|
FieldItemBase:: |
public | function |
Implements ContextAwareInterface::setParent(). Overrides ContextAwareInterface:: |
|
FieldItemBase:: |
public | function |
Implements ComplexDataInterface::setPropertyValues(). Overrides ComplexDataInterface:: |
|
FieldItemBase:: |
public | function |
Implements TypedDataInterface::setValue(). Overrides TypedData:: |
3 |
FieldItemBase:: |
public | function |
Implements TypedDataInterface::validate(). Overrides TypedDataInterface:: |
|
FieldItemBase:: |
public | function | Implements a deep clone. | |
FieldItemBase:: |
public | function |
Implements TypedDataInterface::__construct(). Overrides TypedData:: |
|
FieldItemBase:: |
public | function |
Implements FieldItemInterface::__get(). Overrides FieldItemInterface:: |
|
FieldItemBase:: |
public | function |
Implements FieldItemInterface::__isset(). Overrides FieldItemInterface:: |
|
FieldItemBase:: |
public | function |
Implements FieldItemInterface::__set(). Overrides FieldItemInterface:: |
|
FieldItemBase:: |
public | function |
Implements FieldItemInterface::__unset(). Overrides FieldItemInterface:: |
|
TypedData:: |
protected | property | The data definition. | |
TypedData:: |
public | function |
Implements TypedDataInterface::getDefinition(). Overrides TypedDataInterface:: |
|
TypedData:: |
public | function |
Implements TypedDataInterface::getType(). Overrides TypedDataInterface:: |