An entity field, i.e. a list of field items.
An entity field is a list of field items, which contain only primitive properties or entity references. Note that even single-valued entity fields are represented as list of items, however for easy access to the contained item the entity field delegates __get() and __set() calls directly to the first item.
Expanded class hierarchy of Field
\Drupal\Core\Entity\Field\FieldInterface
class Field extends TypedData implements IteratorAggregate, FieldInterface {
/**
* The entity field name.
*
* @var string
*/
protected $name;
/**
* The parent entity.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
protected $parent;
/**
* Numerically indexed array of field items, implementing the
* FieldItemInterface.
*
* @var array
*/
protected $list = array();
/**
* Implements TypedDataInterface::getValue().
*/
public function getValue() {
$values = array();
foreach ($this->list as $delta => $item) {
$values[$delta] = !$item
->isEmpty() ? $item
->getValue() : NULL;
}
return $values;
}
/**
* Implements TypedDataInterface::setValue().
*
* @param array $values
* An array of values of the field items.
*/
public function setValue($values) {
if (isset($values) && $values !== array()) {
// Support passing in only the value of the first item.
if (!is_array($values) || !is_numeric(current(array_keys($values)))) {
$values = array(
0 => $values,
);
}
if (!is_array($values)) {
throw new InvalidArgumentException("An entity field requires a numerically indexed array of items as value.");
}
// Clear the values of properties for which no value has been passed.
foreach (array_diff_key($this->list, $values) as $delta => $item) {
unset($this->list[$delta]);
}
// Set the values.
foreach ($values as $delta => $value) {
if (!is_numeric($delta)) {
throw new InvalidArgumentException('Unable to set a value with a non-numeric delta in a list.');
}
elseif (!isset($this->list[$delta])) {
$this->list[$delta] = $this
->createItem($value);
}
else {
$this->list[$delta]
->setValue($value);
}
}
}
else {
$this->list = array();
}
}
/**
* Returns a string representation of the field.
*
* @return string
*/
public function getString() {
$strings = array();
foreach ($this
->list() as $item) {
$strings[] = $item
->getString();
}
return implode(', ', array_filter($strings));
}
/**
* Implements TypedDataInterface::validate().
*/
public function validate() {
// @todo implement
}
/**
* Implements ArrayAccess::offsetExists().
*/
public function offsetExists($offset) {
return array_key_exists($offset, $this->list);
}
/**
* Implements ArrayAccess::offsetUnset().
*/
public function offsetUnset($offset) {
unset($this->list[$offset]);
}
/**
* Implements ArrayAccess::offsetGet().
*/
public function offsetGet($offset) {
if (!is_numeric($offset)) {
throw new InvalidArgumentException('Unable to get a value with a non-numeric delta in a list.');
}
elseif (!isset($this->list[$offset])) {
$this->list[$offset] = $this
->createItem();
}
return $this->list[$offset];
}
/**
* Helper for creating a list item object.
*
* @return \Drupal\Core\TypedData\TypedDataInterface
*/
protected function createItem($value = NULL) {
$context = array(
'parent' => $this,
);
return typed_data()
->create(array(
'list' => FALSE,
) + $this->definition, $value, $context);
}
/**
* Implements ArrayAccess::offsetSet().
*/
public function offsetSet($offset, $value) {
if (!isset($offset)) {
// The [] operator has been used so point at a new entry.
$offset = $this->list ? max(array_keys($this->list)) + 1 : 0;
}
if (is_numeric($offset)) {
// Support setting values via typed data objects.
if ($value instanceof TypedDataInterface) {
$value = $value
->getValue();
}
$this
->offsetGet($offset)
->setValue($value);
}
else {
throw new InvalidArgumentException('Unable to set a value with a non-numeric delta in a list.');
}
}
/**
* Implements IteratorAggregate::getIterator().
*/
public function getIterator() {
return new ArrayIterator($this->list);
}
/**
* Implements Countable::count().
*/
public function count() {
return count($this->list);
}
/**
* 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\EntityInterface
*/
public function getParent() {
return $this->parent;
}
/**
* Implements ContextAwareInterface::setParent().
*/
public function setParent($parent) {
$this->parent = $parent;
}
/**
* Delegate.
*/
public function getPropertyDefinition($name) {
return $this
->offsetGet(0)
->getPropertyDefinition($name);
}
/**
* Delegate.
*/
public function getPropertyDefinitions() {
return $this
->offsetGet(0)
->getPropertyDefinitions();
}
/**
* Delegate.
*/
public function __get($property_name) {
return $this
->offsetGet(0)
->__get($property_name);
}
/**
* Delegate.
*/
public function get($property_name) {
return $this
->offsetGet(0)
->get($property_name);
}
/**
* Delegate.
*/
public function __set($property_name, $value) {
$this
->offsetGet(0)
->__set($property_name, $value);
}
/**
* Delegate.
*/
public function __isset($property_name) {
return $this
->offsetGet(0)
->__isset($property_name);
}
/**
* Delegate.
*/
public function __unset($property_name) {
return $this
->offsetGet(0)
->__unset($property_name);
}
/**
* Implements ListInterface::isEmpty().
*/
public function isEmpty() {
foreach ($this->list as $item) {
if (!$item
->isEmpty()) {
return FALSE;
}
}
return TRUE;
}
/**
* Implements a deep clone.
*/
public function __clone() {
foreach ($this->list as $delta => $property) {
$this->list[$delta] = clone $property;
}
}
/**
* Implements AccessibleInterface::access().
*/
public function access($operation = 'view', User $account = NULL) {
// TODO: Implement access() method. Use item access.
}
}
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Field:: |
protected | property | Numerically indexed array of field items, implementing the FieldItemInterface. | |
Field:: |
protected | property | The entity field name. | |
Field:: |
protected | property | The parent entity. | |
Field:: |
public | function |
Implements AccessibleInterface::access(). Overrides AccessibleInterface:: |
|
Field:: |
public | function | Implements Countable::count(). | |
Field:: |
protected | function | Helper for creating a list item object. | |
Field:: |
public | function |
Delegate. Overrides FieldInterface:: |
|
Field:: |
public | function | Implements IteratorAggregate::getIterator(). | |
Field:: |
public | function |
Implements ContextAwareInterface::getName(). Overrides ContextAwareInterface:: |
|
Field:: |
public | function |
Implements ContextAwareInterface::getParent(). Overrides ContextAwareInterface:: |
|
Field:: |
public | function |
Delegate. Overrides FieldInterface:: |
|
Field:: |
public | function |
Delegate. Overrides FieldInterface:: |
|
Field:: |
public | function |
Returns a string representation of the field. Overrides TypedData:: |
|
Field:: |
public | function |
Implements TypedDataInterface::getValue(). Overrides TypedData:: |
|
Field:: |
public | function |
Implements ListInterface::isEmpty(). Overrides ListInterface:: |
|
Field:: |
public | function | Implements ArrayAccess::offsetExists(). | |
Field:: |
public | function | Implements ArrayAccess::offsetGet(). | |
Field:: |
public | function | Implements ArrayAccess::offsetSet(). | |
Field:: |
public | function | Implements ArrayAccess::offsetUnset(). | |
Field:: |
public | function |
Implements ContextAwareInterface::setName(). Overrides ContextAwareInterface:: |
|
Field:: |
public | function |
Implements ContextAwareInterface::setParent(). Overrides ContextAwareInterface:: |
|
Field:: |
public | function |
Implements TypedDataInterface::setValue(). Overrides TypedData:: |
|
Field:: |
public | function |
Implements TypedDataInterface::validate(). Overrides TypedDataInterface:: |
|
Field:: |
public | function | Implements a deep clone. | |
Field:: |
public | function |
Delegate. Overrides FieldInterface:: |
|
Field:: |
public | function |
Delegate. Overrides FieldInterface:: |
|
Field:: |
public | function |
Delegate. Overrides FieldInterface:: |
|
Field:: |
public | function |
Delegate. Overrides FieldInterface:: |
|
TypedData:: |
protected | property | The data definition. | |
TypedData:: |
public | function |
Implements TypedDataInterface::getDefinition(). Overrides TypedDataInterface:: |
|
TypedData:: |
public | function |
Implements TypedDataInterface::getType(). Overrides TypedDataInterface:: |
|
TypedData:: |
public | function | Constructs a TypedData object given its definition. | 3 |