interface FieldItemInterface implements ComplexDataInterface (View source)

Interface for entity field items.

Entity field items are typed data objects containing the field values, i.e. implementing the ComplexDataInterface.

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.

get($property_name)

Gets a property object.

$this
set($property_name, $value, bool $notify = TRUE)

Sets a property value.

getProperties(bool $include_computed = FALSE)

Gets an array of property objects.

array
toArray()

Returns an array of all property values.

bool
isEmpty()

Determines whether the data structure is empty.

propertyDefinitions(FieldStorageDefinitionInterface $field_definition)

Defines field item properties.

static string|null
mainPropertyName()

Returns the name of the main property, if any.

static array
schema(FieldStorageDefinitionInterface $field_definition)

Returns the schema for the field.

getEntity()

Gets the entity that field belongs to.

string
getLangcode()

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

getFieldDefinition()

Gets the field definition.

mixed
__get(string $property_name)

Magic method: Gets a property value.

__set(string $property_name, mixed $value)

Magic method: Sets a property value.

bool
__isset(string $property_name)

Magic method: Determines whether a property is set.

__unset(string $property_name)

Magic method: Unsets a property.

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

Returns a renderable array for a single 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.

static array
generateSampleValue(FieldDefinitionInterface $field_definition)

Generates placeholder field values.

static array
defaultStorageSettings()

Defines the storage-level settings for this plugin.

static array
defaultFieldSettings()

Defines the field-level settings for this plugin.

static array
storageSettingsToConfigData(array $settings)

Returns a settings array that can be stored as a configuration value.

static array
storageSettingsFromConfigData(array $settings)

Returns a settings array in the field type's canonical representation.

static array
fieldSettingsToConfigData(array $settings)

Returns a settings array that can be stored as a configuration value.

static array
fieldSettingsFromConfigData(array $settings)

Returns a settings array in the field type's canonical representation.

array
storageSettingsForm(array $form, FormStateInterface $form_state, bool $has_data)

Returns a form for the storage-level settings.

array
fieldSettingsForm(array $form, FormStateInterface $form_state)

Returns a form for the field-level settings.

static array
calculateDependencies(FieldDefinitionInterface $field_definition)

Calculates dependencies for field items.

static array
calculateStorageDependencies(FieldStorageDefinitionInterface $field_storage_definition)

Calculates dependencies for field items on the storage level.

static bool
onDependencyRemoval(FieldDefinitionInterface $field_definition, array $dependencies)

Informs the plugin that a dependency of the field will be deleted.

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.

TypedDataInterface get($property_name)

Gets a property object.

Parameters

$property_name

The name of the property to get; e.g., 'title' or 'name'.

Return Value

TypedDataInterface

The property object.

Exceptions

InvalidArgumentException
MissingDataException

$this set($property_name, $value, bool $notify = TRUE)

Sets a property value.

Parameters

$property_name

The name of the property to set; e.g., 'title' or 'name'.

$value

The value to set, or NULL to unset the property.

bool $notify

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

Return Value

$this

Exceptions

InvalidArgumentException
MissingDataException

TypedDataInterface[] getProperties(bool $include_computed = FALSE)

Gets an array of property objects.

Parameters

bool $include_computed

If set to TRUE, computed properties are included. Defaults to FALSE.

Return Value

TypedDataInterface[]

An array of property objects implementing the TypedDataInterface, keyed by property name.

Exceptions

MissingDataException

array toArray()

Returns an array of all property values.

Gets an array of plain property values including all not-computed properties.

Return Value

array

An array of property values, keyed by property name.

Exceptions

MissingDataException

bool isEmpty()

Determines whether the data structure is empty.

Return Value

bool

TRUE if the data structure is empty, FALSE otherwise.

static DataDefinitionInterface[] propertyDefinitions(FieldStorageDefinitionInterface $field_definition)

Defines field item properties.

Properties that are required to constitute a valid, non-empty item should be denoted with \Drupal\Core\TypedData\DataDefinition::setRequired().

Parameters

FieldStorageDefinitionInterface $field_definition

Return Value

DataDefinitionInterface[]

An array of property definitions of contained properties, keyed by property name.

See also

BaseFieldDefinition

static string|null mainPropertyName()

Returns the name of the main property, if any.

Some field items consist mainly of one main property, e.g. the value of a text field or the target_id of an entity reference. If the field item has no main property, the method returns NULL.

Return Value

string|null

The name of the value property, or NULL if there is none.

See also

BaseFieldDefinition

static array schema(FieldStorageDefinitionInterface $field_definition)

Returns the schema for the field.

This method is static because the field schema information is needed on creation of the field. FieldItemInterface objects instantiated at that time are not reliable as field settings might be missing.

Computed fields having no schema should return an empty array.

Parameters

FieldStorageDefinitionInterface $field_definition

The field definition.

Return Value

array

An empty array if there is no schema, or an associative array with the following key/value pairs:

  • columns: An array of Schema API column specifications, keyed by column name. The columns need to be a subset of the properties defined in propertyDefinitions(). The 'not null' property is ignored if present, as it is determined automatically by the storage controller depending on the table layout and the property definitions. It is recommended to avoid having the column definitions depend on field settings when possible. No assumptions should be made on how storage engines internally use the original column name to structure their storage.
  • unique keys: (optional) An array of Schema API unique key definitions. Only columns that appear in the 'columns' array are allowed.
  • indexes: (optional) An array of Schema API index definitions. Only columns that appear in the 'columns' array are allowed. Those indexes will be used as default indexes. Field definitions can specify additional indexes or, at their own risk, modify the default indexes specified by the field-type module. Some storage engines might not support indexes.
  • foreign keys: (optional) An array of Schema API foreign key definitions. Note, however, that the field data is not necessarily stored in SQL. Also, the possible usage is limited, as you cannot specify another field as related, only existing SQL tables, such as {taxonomy_term_data}.

FieldableEntityInterface getEntity()

Gets the entity that field belongs to.

Return Value

FieldableEntityInterface

The entity object.

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.

mixed __get(string $property_name)

Magic method: Gets a property value.

Parameters

string $property_name

The name of the property to get; e.g., 'title' or 'name'.

Return Value

mixed

The property value.

Exceptions

InvalidArgumentException

__set(string $property_name, mixed $value)

Magic method: Sets a property value.

Parameters

string $property_name

The name of the property to set; e.g., 'title' or 'name'.

mixed $value

The value to set, or NULL to unset the property. Optionally, a typed data object implementing Drupal\Core\TypedData\TypedDataInterface may be passed instead of a plain value.

Exceptions

InvalidArgumentException

bool __isset(string $property_name)

Magic method: Determines whether a property is set.

Parameters

string $property_name

The name of the property to get; e.g., 'title' or 'name'.

Return Value

bool

Returns TRUE if the property exists and is set, FALSE otherwise.

__unset(string $property_name)

Magic method: Unsets a property.

Parameters

string $property_name

The name of the property to get; e.g., 'title' or 'name'.

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

Returns a renderable array for a single field item.

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 item.

See also

EntityViewBuilderInterface::viewField
EntityViewBuilderInterface::viewFieldItem
FieldItemListInterface::view

preSave()

Defines custom presave behavior for field values.

This method is called during the process of saving an entity, just before values are written into storage. When storing a new entity, its identifier will not be available yet. This should be used to massage item property values or perform any other operation that needs to happen before values are stored. For instance this is the proper phase to auto-create a new entity for an entity reference field item, because this way it will be possible to store the referenced entity identifier.

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 values are written into storage. This is useful mostly when the business logic to be implemented always requires the entity identifier, even when storing a new entity. For instance, when implementing circular entity references, the referenced entity will be created on pre-save with a dummy value for the referring entity identifier, which will be updated with the actual one on post-save.

In the rare cases where item properties depend on the entity identifier, massaging logic will have to be implemented on post-save and returning TRUE will allow them to be rewritten to the storage with the updated values.

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.

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.

static array generateSampleValue(FieldDefinitionInterface $field_definition)

Generates placeholder field values.

Useful when populating site with placeholder content during site building or profiling.

Parameters

FieldDefinitionInterface $field_definition

The field definition.

Return Value

array

An associative array of values.

static array defaultStorageSettings()

Defines the storage-level settings for this plugin.

Return Value

array

A list of default settings, keyed by the setting name.

static array defaultFieldSettings()

Defines the field-level settings for this plugin.

Return Value

array

A list of default settings, keyed by the setting name.

static array storageSettingsToConfigData(array $settings)

Returns a settings array that can be stored as a configuration value.

For all use cases where field settings are stored and managed as configuration, this method is used to map from the field type's representation of its settings to a representation compatible with deployable configuration. This includes:

  • Array keys at any depth must not contain a ".".
  • Ideally, array keys at any depth are either numeric or can be enumerated as a "mapping" within the configuration schema. While not strictly required, this simplifies configuration translation UIs, configuration migrations between Drupal versions, and other use cases.
  • To support configuration deployments, references to content entities must use UUIDs rather than local IDs.

An example of a conversion between representations might be an "allowed_values" setting that's structured by the field type as a \Drupal\Core\TypedData\OptionsProviderInterface::getPossibleOptions() result (i.e., values as keys and labels as values). For such a use case, in order to comply with the above, this method could convert that representation to a numerically indexed array whose values are sub-arrays with the schema definable keys of "value" and "label".

Parameters

array $settings

The field's settings in the field type's canonical representation.

Return Value

array

An array (either the unmodified $settings or a modified representation) that is suitable for storing as a deployable configuration value.

See also

Config::set

static array storageSettingsFromConfigData(array $settings)

Returns a settings array in the field type's canonical representation.

This function does the inverse of static::storageSettingsToConfigData(). It's called when loading a field's settings from a configuration object.

Parameters

array $settings

The field's settings, as it is stored within a configuration object.

Return Value

array

The settings, in the representation expected by the field type and code that interacts with it.

See also

FieldItemInterface::storageSettingsToConfigData

static array fieldSettingsToConfigData(array $settings)

Returns a settings array that can be stored as a configuration value.

Same as static::storageSettingsToConfigData(), but for the field's settings.

Parameters

array $settings

The field's settings in the field type's canonical representation.

Return Value

array

An array (either the unmodified $settings or a modified representation) that is suitable for storing as a deployable configuration value.

See also

FieldItemInterface::storageSettingsToConfigData

static array fieldSettingsFromConfigData(array $settings)

Returns a settings array in the field type's canonical representation.

This function does the inverse of static::fieldSettingsToConfigData(). It's called when loading a field's settings from a configuration object.

Parameters

array $settings

The field's settings, as it is stored within a configuration object.

Return Value

array

The field settings, in the representation expected by the field type and code that interacts with it.

See also

FieldItemInterface::fieldSettingsToConfigData

array storageSettingsForm(array $form, FormStateInterface $form_state, bool $has_data)

Returns a form for the storage-level settings.

Invoked from \Drupal\field_ui\Form\FieldStorageConfigEditForm to allow administrators to configure storage-level settings.

Field storage might reject settings changes that affect the field storage schema if the storage already has data. When the $has_data parameter is TRUE, the form should not allow changing the settings that take part in the schema() method. It is recommended to set #access to FALSE on the corresponding elements.

Parameters

array $form

The form where the settings form is being included in.

FormStateInterface $form_state

The form state of the (entire) configuration form.

bool $has_data

TRUE if the field already has data, FALSE if not.

Return Value

array

The form definition for the field settings.

array fieldSettingsForm(array $form, FormStateInterface $form_state)

Returns a form for the field-level settings.

Invoked from \Drupal\field_ui\Form\FieldConfigEditForm to allow administrators to configure field-level settings.

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 settings.

static array calculateDependencies(FieldDefinitionInterface $field_definition)

Calculates dependencies for field items.

Dependencies are saved in the field configuration entity and are used to determine configuration synchronization order. For example, if the field type's default value is a content entity, this method should return an array of dependencies listing the content entities.

Parameters

FieldDefinitionInterface $field_definition

The field definition.

Return Value

array

An array of dependencies grouped by type (config, content, module, theme). For example: @code array( 'config' => array('user.role.anonymous', 'user.role.authenticated'), 'content' => array('node:article:f0a189e6-55fb-47fb-8005-5bef81c44d6d'), 'module' => array('node', 'user'), 'theme' => array('seven'), ); @endcode

See also

ConfigDependencyManager
\Drupal\Core\Config\Entity\ConfigEntityInterface::getConfigDependencyName()

static array calculateStorageDependencies(FieldStorageDefinitionInterface $field_storage_definition)

Calculates dependencies for field items on the storage level.

Dependencies are saved in the field storage configuration entity and are used to determine configuration synchronization order. For example, if the field type storage depends on a particular entity type, this method should return an array of dependencies listing the module that provides the entity type.

Dependencies returned from this method are stored in field storage configuration and are always considered hard dependencies. If the dependency is removed the field storage configuration must be deleted.

Parameters

FieldStorageDefinitionInterface $field_storage_definition

The field storage definition.

Return Value

array

An array of dependencies grouped by type (config, content, module, theme). For example: @code [ 'config' => ['user.role.anonymous', 'user.role.authenticated'], 'content' => ['node:article:f0a189e6-55fb-47fb-8005-5bef81c44d6d'], 'module' => ['node', 'user'], 'theme' => ['seven'], ]; @endcode

See also

ConfigDependencyManager
\Drupal\Core\Config\Entity\ConfigEntityInterface::getConfigDependencyName()

static bool onDependencyRemoval(FieldDefinitionInterface $field_definition, array $dependencies)

Informs the plugin that a dependency of the field will be deleted.

Parameters

FieldDefinitionInterface $field_definition

The field definition.

array $dependencies

An array of dependencies that will be deleted keyed by dependency type. Dependency types are, for example, entity, module and theme.

Return Value

bool

TRUE if the field definition has been changed as a result, FALSE if not.

See also

\Drupal\Core\Config\ConfigEntityInterface::onDependencyRemoval()