interface FieldItemListInterface implements ListInterface, AccessibleInterface (View source)

Interface for fields, being lists of field items.

This interface must be implemented by every entity field, whereas contained field items must implement the FieldItemInterface. Some methods of the fields are delegated to the first contained item, in particular get() and set() as well as their magic equivalences.

Optionally, a typed data object implementing Drupal\Core\TypedData\TypedDataInterface may be passed to ArrayAccess::offsetSet() instead of a plain value.

When implementing this interface which extends Traversable, make sure to list IteratorAggregate or Iterator before this interface in the implements clause.

Methods

static 
createInstance(DataDefinitionInterface $definition, string|null $name = NULL, TraversableTypedDataInterface $parent = NULL)

Constructs a TypedData object given its definition and context.

mixed
getValue()

Gets the data value.

setValue(mixed|null $value, bool $notify = TRUE)

Sets the data value.

string
getString()

Returns a string representation of the data.

array
getConstraints()

Gets a list of validation constraints.

ConstraintViolationListInterface
validate()

Validates the currently set data value.

$this
applyDefaultValue(bool $notify = TRUE)

Applies the default value.

string|int|null
getName()

Returns the name of a property or item.

getParent()

Returns the parent data structure; i.e. either complex data or a list.

getRoot()

Returns the root of the typed data tree.

string
getPropertyPath()

Returns the property path of the data.

setContext(string|null $name = NULL, TraversableTypedDataInterface $parent = NULL)

Sets the context of a property or item via a context aware parent.

onChange($name)

React to changes to a child property or item.

bool
isEmpty()

Determines whether the list contains any non-empty items.

getItemDefinition()

Gets the definition of a contained item.

get(int $index)

Returns the item at the specified position in this list.

$this
set(int $index, mixed $value)

Sets the value of the item at a given position in the list.

first()

Returns the first item in this list.

appendItem(mixed $value = NULL)

Appends a new item to the list.

$this
removeItem(int $index)

Removes the item at the specified position.

$this
filter(callable $callback)

Filters the items in the list using a custom callback.

access(string $operation, AccountInterface $account = NULL, bool $return_as_object = FALSE)

Checks data value access.

getEntity()

Gets the entity that field belongs to.

setLangcode(string $langcode)

Sets the langcode of the field values held in the object.

string
getLangcode()

Gets the langcode of the field values held in the object.

getFieldDefinition()

Gets the field definition.

array
getSettings()

Returns the array of field settings.

mixed
getSetting(string $setting_name)

Returns the value of a given field setting.

defaultAccess($operation = 'view', AccountInterface $account = NULL)

Contains the default access logic of this field.

$this
filterEmptyItems()

Filters out empty field items and re-numbers the item deltas.

__get($property_name)

Magic method: Gets a property value of to the first field item.

__set($property_name, $value)

Magic method: Sets a property value of the first field item.

__isset($property_name)

Magic method: Determines whether a property of the first field item is set.

__unset($property_name)

Magic method: Unsets a property of the first field item.

preSave()

Defines custom presave behavior for field values.

bool
postSave(bool $update)

Defines custom post-save behavior for field values.

delete()

Defines custom delete behavior for field values.

deleteRevision()

Defines custom revision delete behavior for field values.

array
view(string|array $display_options = [])

Returns a renderable array for the field items.

generateSampleItems(int $count = 1)

Populates a specified number of field items with valid sample data.

array
defaultValuesForm(array $form, FormStateInterface $form_state)

Returns a form for the default value input.

defaultValuesFormValidate(array $element, array $form, FormStateInterface $form_state)

Validates the submitted default value.

array
defaultValuesFormSubmit(array $element, array $form, FormStateInterface $form_state)

Processes the submitted default value.

static array
processDefaultValue(array $default_value, FieldableEntityInterface $entity, FieldDefinitionInterface $definition)

Processes the default value before being applied.

bool
equals(FieldItemListInterface $list_to_compare)

Determines equality to another object implementing FieldItemListInterface.

bool
hasAffectingChanges(FieldItemListInterface $original_items, string $langcode)

Determines whether the field has relevant changes.

Details

static createInstance(DataDefinitionInterface $definition, string|null $name = NULL, TraversableTypedDataInterface $parent = NULL)

Constructs a TypedData object given its definition and context.

When \Drupal\Core\Config\TypedConfigManager has been fixed to use class-based definitions, type-hint $definition to DataDefinitionInterface. https://www.drupal.org/node/1928868

Parameters

DataDefinitionInterface $definition

The data definition.

string|null $name

(optional) The name of the created property, or NULL if it is the root of a typed data tree. Defaults to NULL.

TraversableTypedDataInterface $parent

(optional) The parent object of the data property, or NULL if it is the root of a typed data tree. Defaults to NULL.

See also

TypedDataManager::create

DataDefinitionInterface getDataDefinition()

Gets the data definition.

Return Value

DataDefinitionInterface

The data definition object.

mixed getValue()

Gets the data value.

Return Value

mixed

The data value.

setValue(mixed|null $value, bool $notify = TRUE)

Sets the data value.

Parameters

mixed|null $value

The value to set in the format as documented for the data type or NULL to unset the data value.

bool $notify

(optional) Whether to notify the parent object of the change. Defaults to TRUE. If a property is updated from a parent object, set it to FALSE to avoid being notified again.

Exceptions

InvalidArgumentException
ReadOnlyException

string getString()

Returns a string representation of the data.

Return Value

string

The string representation of the data.

array getConstraints()

Gets a list of validation constraints.

Return Value

array

Array of constraints, each being an instance of \Symfony\Component\Validator\Constraint.

ConstraintViolationListInterface validate()

Validates the currently set data value.

Return Value

ConstraintViolationListInterface

A list of constraint violations. If the list is empty, validation succeeded.

$this applyDefaultValue(bool $notify = TRUE)

Applies the default value.

Parameters

bool $notify

(optional) Whether to notify the parent object of the change. Defaults to TRUE. If a property is updated from a parent object, set it to FALSE to avoid being notified again.

Return Value

$this

Returns itself to allow for chaining.

string|int|null getName()

Returns the name of a property or item.

Return Value

string|int|null

If the data is a property of some complex data, the name of the property. If the data is an item of a list, the name is the numeric position of the item in the list, starting with 0. Otherwise, NULL is returned.

TraversableTypedDataInterface|null getParent()

Returns the parent data structure; i.e. either complex data or a list.

Return Value

TraversableTypedDataInterface|null

The parent data structure, either complex data or a list; or NULL if this is the root of the typed data tree.

TraversableTypedDataInterface getRoot()

Returns the root of the typed data tree.

Returns the root data for a tree of typed data objects; e.g. for an entity field item the root of the tree is its parent entity object.

Return Value

TraversableTypedDataInterface

The root data structure, either complex data or a list.

string getPropertyPath()

Returns the property path of the data.

The trail of property names relative to the root of the typed data tree, separated by dots; e.g. 'field_text.0.format'.

Return Value

string

The property path relative to the root of the typed tree, or an empty string if this is the root.

setContext(string|null $name = NULL, TraversableTypedDataInterface $parent = NULL)

Sets the context of a property or item via a context aware parent.

This method is supposed to be called by the factory only.

Parameters

string|null $name

(optional) The name of the property or the delta of the list item, or NULL if it is the root of a typed data tree. Defaults to NULL.

TraversableTypedDataInterface $parent

(optional) The parent object of the data property, or NULL if it is the root of a typed data tree. Defaults to NULL.

onChange($name)

React to changes to a child property or item.

Note that this is invoked after any changes have been applied.

Parameters

$name

The name of the property or the delta of the list item which is changed.

bool isEmpty()

Determines whether the list contains any non-empty items.

Return Value

bool

TRUE if the list is empty, FALSE otherwise.

DataDefinitionInterface getItemDefinition()

Gets the definition of a contained item.

Return Value

DataDefinitionInterface

The data definition of contained items.

TypedDataInterface|null get(int $index)

Returns the item at the specified position in this list.

Parameters

int $index

Index of the item to return.

Return Value

TypedDataInterface|null

The item at the specified position in this list, or NULL if no item exists at that position.

Exceptions

MissingDataException

$this set(int $index, mixed $value)

Sets the value of the item at a given position in the list.

Parameters

int $index

The position of the item in the list. Since a List only contains sequential, 0-based indexes, $index has to be:

  • Either the position of an existing item in the list. This updates the item value.
  • Or the next available position in the sequence of the current list indexes. This appends a new item with the provided value at the end of the list.
mixed $value

The value of the item to be stored at the specified position.

Return Value

$this

Exceptions

InvalidArgumentException
MissingDataException

TypedDataInterface|null first()

Returns the first item in this list.

Return Value

TypedDataInterface|null

The first item in this list, or NULL if there are no items.

Exceptions

MissingDataException

TypedDataInterface appendItem(mixed $value = NULL)

Appends a new item to the list.

Parameters

mixed $value

The value of the new item.

Return Value

TypedDataInterface

The item that was appended.

$this removeItem(int $index)

Removes the item at the specified position.

Parameters

int $index

Index of the item to remove.

Return Value

$this

$this filter(callable $callback)

Filters the items in the list using a custom callback.

Parameters

callable $callback

The callback to use for filtering. Like with array_filter(), the callback is called for each item in the list. Only items for which the callback returns TRUE are preserved.

Return Value

$this

bool|AccessResultInterface access(string $operation, AccountInterface $account = NULL, bool $return_as_object = FALSE)

Checks data value access.

Parameters

string $operation

The operation to be performed.

AccountInterface $account

(optional) The user for which to check access, or NULL to check access for the current user. Defaults to NULL.

bool $return_as_object

(optional) Defaults to FALSE.

Return Value

bool|AccessResultInterface

The access result. Returns a boolean if $return_as_object is FALSE (this is the default) and otherwise an AccessResultInterface object. When a boolean is returned, the result of AccessInterface::isAllowed() is returned, i.e. TRUE means access is explicitly allowed, FALSE means access is either explicitly forbidden or "no opinion".

FieldableEntityInterface getEntity()

Gets the entity that field belongs to.

Return Value

FieldableEntityInterface

The entity object. If the entity is translatable and a specific translation is required, always request it by calling ::getTranslation() or ::getUntranslated() as the language of the returned object is not defined.

setLangcode(string $langcode)

Sets the langcode of the field values held in the object.

Parameters

string $langcode

The langcode.

string getLangcode()

Gets the langcode of the field values held in the object.

Return Value

string

The langcode.

FieldDefinitionInterface getFieldDefinition()

Gets the field definition.

Return Value

FieldDefinitionInterface

The field definition.

array getSettings()

Returns the array of field settings.

Return Value

array

An array of key/value pairs.

mixed getSetting(string $setting_name)

Returns the value of a given field setting.

Parameters

string $setting_name

The setting name.

Return Value

mixed

The setting value.

AccessResultInterface defaultAccess($operation = 'view', AccountInterface $account = NULL)

Contains the default access logic of this field.

See \Drupal\Core\Entity\EntityAccessControlHandlerInterface::fieldAccess() for the parameter documentation.

Parameters

$operation
AccountInterface $account

Return Value

AccessResultInterface

The access result.

$this filterEmptyItems()

Filters out empty field items and re-numbers the item deltas.

Return Value

$this

__get($property_name)

Magic method: Gets a property value of to the first field item.

Parameters

$property_name

See also

FieldItemInterface::__set

__set($property_name, $value)

Magic method: Sets a property value of the first field item.

Parameters

$property_name
$value

See also

FieldItemInterface::__get

__isset($property_name)

Magic method: Determines whether a property of the first field item is set.

Parameters

$property_name

See also

FieldItemInterface::__unset

__unset($property_name)

Magic method: Unsets a property of the first field item.

Parameters

$property_name

See also

FieldItemInterface::__isset

preSave()

Defines custom presave behavior for field values.

This method is called during the process of saving an entity, just before item values are written into storage.

bool postSave(bool $update)

Defines custom post-save behavior for field values.

This method is called during the process of saving an entity, just after item values are written into storage.

Parameters

bool $update

Specifies whether the entity is being updated or created.

Return Value

bool

Whether field items should be rewritten to the storage as a consequence of the logic implemented by the custom behavior.

See also

FieldItemInterface::postSave

delete()

Defines custom delete behavior for field values.

This method is called during the process of deleting an entity, just before values are deleted from storage.

deleteRevision()

Defines custom revision delete behavior for field values.

This method is called from during the process of deleting an entity revision, just before the field values are deleted from storage. It is only called for entity types that support revisioning.

array view(string|array $display_options = [])

Returns a renderable array for the field items.

Parameters

string|array $display_options

Can be either the name of a view mode, or an array of display settings. See EntityViewBuilderInterface::viewField() for more information.

Return Value

array

A renderable array for the field values.

See also

EntityViewBuilderInterface::viewField
FieldItemInterface::view

generateSampleItems(int $count = 1)

Populates a specified number of field items with valid sample data.

Parameters

int $count

The number of items to create.

array defaultValuesForm(array $form, FormStateInterface $form_state)

Returns a form for the default value input.

Invoked from \Drupal\field_ui\Form\FieldConfigEditForm to allow administrators to configure instance-level default value.

Parameters

array $form

The form where the settings form is being included in.

FormStateInterface $form_state

The form state of the (entire) configuration form.

Return Value

array

The form definition for the field default value.

defaultValuesFormValidate(array $element, array $form, FormStateInterface $form_state)

Validates the submitted default value.

Invoked from \Drupal\field_ui\Form\FieldConfigEditForm to allow administrators to configure instance-level default value.

Parameters

array $element

The default value form element.

array $form

The form where the settings form is being included in.

FormStateInterface $form_state

The form state of the (entire) configuration form.

array defaultValuesFormSubmit(array $element, array $form, FormStateInterface $form_state)

Processes the submitted default value.

Invoked from \Drupal\field_ui\Form\FieldConfigEditForm to allow administrators to configure instance-level default value.

Parameters

array $element

The default value form element.

array $form

The form where the settings form is being included in.

FormStateInterface $form_state

The form state of the (entire) configuration form.

Return Value

array

The field default value.

static array processDefaultValue(array $default_value, FieldableEntityInterface $entity, FieldDefinitionInterface $definition)

Processes the default value before being applied.

Defined or configured default values of a field might need some processing in order to be a valid runtime value for the field type; e.g., a date field could process the defined value of 'NOW' to a valid date.

Parameters

array $default_value

The unprocessed default value defined for the field, as a numerically indexed array of items, each item being an array of property/value pairs.

FieldableEntityInterface $entity

The entity for which the default value is generated.

FieldDefinitionInterface $definition

The definition of the field.

Return Value

array

The return default value for the field.

bool equals(FieldItemListInterface $list_to_compare)

Determines equality to another object implementing FieldItemListInterface.

This method is usually used by the storage to check for not computed value changes, which will be saved into the storage.

Parameters

FieldItemListInterface $list_to_compare

The field item list to compare to.

Return Value

bool

TRUE if the field item lists are equal, FALSE if not.

bool hasAffectingChanges(FieldItemListInterface $original_items, string $langcode)

Determines whether the field has relevant changes.

This is for example used to determine if a revision of an entity has changes in a given translation. Unlike \Drupal\Core\Field\FieldItemListInterface::equals(), this can report that for example an untranslatable field, despite being changed and therefore technically affecting all translations, is only internal metadata or only affects a single translation.

Parameters

FieldItemListInterface $original_items

The original field items to compare against.

string $langcode

The language that should be checked.

Return Value

bool

TRUE if the field has relevant changes, FALSE if not.