interface FieldDefinitionInterface implements ListDataDefinitionInterface, CacheableDependencyInterface (View source)

Defines an interface for entity field definitions.

An entity field is a data object that holds the values of a particular field for a particular entity (see \Drupal\Core\Field\FieldItemListInterface). For example, $node_1->body and $node_2->body contain different data and therefore are different field objects.

In contrast, an entity field definition is an object that returns information about a field (e.g., its type and settings) rather than its values. As such, if all the information about $node_1->body and $node_2->body is the same, then the same field definition object can be used to describe both.

It is up to the class implementing this interface to manage where the information comes from. For example, field.module provides an implementation based on two levels of configuration. It allows the site administrator to add custom fields to any entity type and bundle via the "field_storage_config" and "field_config" configuration entities. The former for storing configuration that is independent of which entity type and bundle the field is added to, and the latter for storing configuration that is specific to the entity type and bundle. The class that implements "field_config" configuration entities also implements this interface, returning information from either itself, or from the corresponding "field_storage_config" configuration, as appropriate.

However, entity base fields, such as $node->title, are not managed by field.module and its "field_storage_config"/"field_config" configuration entities. Therefore, their definitions are provided by different objects based on the class \Drupal\Core\Field\BaseFieldDefinition, which implements this interface as well.

Field definitions may fully define a concrete data object (e.g., $node_1->body), or may provide a best-guess definition for a data object that might come into existence later. For example, $node_1->body and $node_2->body may have different definitions (e.g., if the node types are different). When adding the "body" field to a View that can return nodes of different types, the View can get a field definition that represents the "body" field abstractly, and present Views configuration options to the administrator based on that abstract definition, even though that abstract definition can differ from the concrete definition of any particular node's body field.

Methods

createFromDataType(string $data_type)

Creates a new data definition object.

string
getDataType()

Returns the data type of the data.

getLabel()

Returns a human readable label.

string|null
getDescription()

Returns a human readable description.

bool
isList()

Returns whether the data is multi-valued, i.e. a list of data items.

bool
isReadOnly()

Determines whether the data is read-only.

bool
isComputed()

Determines whether the data value is computed.

bool
isRequired()

Returns whether the field can be empty.

string
getClass()

Returns the class used for creating the typed data object.

array
getSettings()

Returns the array of settings, as required by the used class.

mixed
getSetting(string $setting_name)

Returns the value of a given setting.

array[]
getConstraints()

Returns an array of validation constraints.

array
getConstraint(string $constraint_name)

Returns a validation constraint.

addConstraint(string $constraint_name, array|null $options = NULL)

Adds a validation constraint.

bool
isInternal()

Determines whether the data value is internal.

createFromItemType(string $item_type)

Creates a new list data definition for items of the given data type.

getItemDefinition()

Gets the data definition of an item of the list.

string[]
getCacheContexts()

The cache contexts associated with this object.

string[]
getCacheTags()

The cache tags associated with this object.

int
getCacheMaxAge()

The maximum age for which this object may be cached.

string
getName()

Returns the machine name of the field.

string
getType()

Returns the field type.

string
getTargetEntityTypeId()

Returns the ID of the entity type the field is attached to.

string|null
getTargetBundle()

Gets the bundle the field is attached to.

bool
isDisplayConfigurable(string $display_context)

Returns whether the display for the field can be configured.

array|null
getDisplayOptions(string $display_context)

Returns the default display options for the field.

array
getDefaultValueLiteral()

Returns the default value literal for the field.

string|null
getDefaultValueCallback()

Returns the default value callback for the field.

array
getDefaultValue(FieldableEntityInterface $entity)

Returns the default value for the field in a newly created entity.

bool
isTranslatable()

Returns whether the field is translatable.

getFieldStorageDefinition()

Returns the field storage definition.

getConfig(string $bundle)

Gets an object that can be saved in configuration.

string
getUniqueIdentifier()

Returns a unique identifier for the field.

Details

static DataDefinitionInterface createFromDataType(string $data_type)

Creates a new data definition object.

This method is typically used by \Drupal\Core\TypedData\TypedDataManager::createDataDefinition() to build a definition object for an arbitrary data type. When the definition class is known, it is recommended to directly use the static create() method on that class instead; e.g.:

Parameters

string $data_type

The data type, for which a data definition should be created.

Return Value

DataDefinitionInterface

Exceptions

InvalidArgumentException

string getDataType()

Returns the data type of the data.

Return Value

string

The data type.

string|TranslatableMarkup getLabel()

Returns a human readable label.

Return Value

string|TranslatableMarkup

The label. A string or an instance of TranslatableMarkup will be returned based on the way the label translation is handled.

string|null getDescription()

Returns a human readable description.

Descriptions are usually used on user interfaces where the data is edited or displayed.

Return Value

string|null

The description, or NULL if no description is available.

bool isList()

Returns whether the data is multi-valued, i.e. a list of data items.

This is equivalent to checking whether the data definition implements the \Drupal\Core\TypedData\ListDefinitionInterface interface.

Return Value

bool

Whether the data is multi-valued.

bool isReadOnly()

Determines whether the data is read-only.

Return Value

bool

Whether the data is read-only.

bool isComputed()

Determines whether the data value is computed.

For example, data could be computed depending on some other values.

Return Value

bool

Whether the data value is computed.

bool isRequired()

Returns whether the field can be empty.

If a field is required, an entity needs to have at least a valid, non-empty item in that field's FieldItemList in order to pass validation.

An item is considered empty if its isEmpty() method returns TRUE. Typically, that is if at least one of its required properties is empty.

Return Value

bool

Whether a data value is required.

See also

ItemList::isEmpty
\Drupal\Core\Field\FieldItemInterface::isEmpty()
DataDefinitionInterface:isRequired()
TypedDataManager::getDefaultConstraints

string getClass()

Returns the class used for creating the typed data object.

If not specified, the default class of the data type will be returned.

Return Value

string

The class used for creating the typed data object.

array getSettings()

Returns the array of settings, as required by the used class.

See the documentation of the class for supported or required settings.

Return Value

array

The array of settings.

mixed getSetting(string $setting_name)

Returns the value of a given setting.

Parameters

string $setting_name

The setting name.

Return Value

mixed

The setting value.

array[] getConstraints()

Returns an array of validation constraints.

The validation constraints of a definition consist of any for it defined constraints and default constraints, which are generated based on the definition and its data type. See \Drupal\Core\TypedData\TypedDataManager::getDefaultConstraints().

Constraints are defined via an array, having constraint plugin IDs as key and constraint options as values, e.g.

Return Value

array[]

An array of validation constraint definitions, keyed by constraint name. Each constraint definition can be used for instantiating \Symfony\Component\Validator\Constraint objects.

See also

ConstraintManager
Constraint

array getConstraint(string $constraint_name)

Returns a validation constraint.

See \Drupal\Core\TypedData\DataDefinitionInterface::getConstraints() for details.

Parameters

string $constraint_name

The name of the constraint, i.e. its plugin id.

Return Value

array

A validation constraint definition which can be used for instantiating a \Symfony\Component\Validator\Constraint object.

See also

Constraint

DataDefinitionInterface addConstraint(string $constraint_name, array|null $options = NULL)

Adds a validation constraint.

See \Drupal\Core\TypedData\DataDefinitionInterface::getConstraints() for details.

Parameters

string $constraint_name

The name of the constraint to add, i.e. its plugin id.

array|null $options

The constraint options as required by the constraint plugin, or NULL.

Return Value

DataDefinitionInterface

The object itself for chaining.

bool isInternal()

Determines whether the data value is internal.

This can be used in a scenario when it is not desirable to expose this data value to an external system.

The implications of this method are left to the discretion of the caller. For example, a module providing an HTTP API may not expose entities of this type, or a custom entity reference field settings form may deprioritize entities of this type in a select list.

Return Value

bool

Whether the data value is internal.

static ListDataDefinitionInterface createFromItemType(string $item_type)

Creates a new list data definition for items of the given data type.

This method is typically used by \Drupal\Core\TypedData\TypedDataManager::createListDataDefinition() to build a definition object for an arbitrary item type. When the definition class is known, it is recommended to directly use the static create() method on that class instead; e.g.:

Parameters

string $item_type

The item type, for which a list data definition should be created.

Return Value

ListDataDefinitionInterface

Exceptions

InvalidArgumentException

DataDefinitionInterface getItemDefinition()

Gets the data definition of an item of the list.

Return Value

DataDefinitionInterface

A data definition describing the list items.

string[] getCacheContexts()

The cache contexts associated with this object.

These identify a specific variation/representation of the object.

Cache contexts are tokens: placeholders that are converted to cache keys by the @cache_contexts_manager service. The replacement value depends on the request context (the current URL, language, and so on). They're converted before storing an object in cache.

Return Value

string[]

An array of cache context tokens, used to generate a cache ID.

See also

CacheContextsManager::convertTokensToKeys

string[] getCacheTags()

The cache tags associated with this object.

When this object is modified, these cache tags will be invalidated.

Return Value

string[]

A set of cache tags.

int getCacheMaxAge()

The maximum age for which this object may be cached.

Return Value

int

The maximum time in seconds that this object may be cached.

string getName()

Returns the machine name of the field.

This defines how the field data is accessed from the entity. For example, if the field name is "foo", then $entity->foo returns its data.

Return Value

string

The field name.

string getType()

Returns the field type.

Return Value

string

The field type, i.e. the id of a field type plugin. For example 'text'.

See also

FieldTypePluginManagerInterface

string getTargetEntityTypeId()

Returns the ID of the entity type the field is attached to.

This method should not be confused with EntityInterface::getEntityTypeId() (configurable fields are config entities, and thus implement both interfaces):

  • FieldDefinitionInterface::getTargetEntityTypeId() answers "as a field, which entity type are you attached to?".
  • EntityInterface::getEntityTypeId() answers "as a (config) entity, what is your own entity type?".

Return Value

string

The entity type ID.

string|null getTargetBundle()

Gets the bundle the field is attached to.

This method should not be confused with EntityInterface::bundle() (configurable fields are config entities, and thus implement both interfaces):

  • FieldDefinitionInterface::getTargetBundle() answers "as a field, which bundle are you attached to?".
  • EntityInterface::bundle() answers "as a (config) entity, what is your own bundle?" (not relevant in our case, the config entity types used to store the definitions of configurable fields do not have bundles).

Return Value

string|null

The bundle the field is defined for, or NULL if it is a base field; i.e., it is not bundle-specific.

bool isDisplayConfigurable(string $display_context)

Returns whether the display for the field can be configured.

Parameters

string $display_context

The display context. Either 'view' or 'form'.

Return Value

bool

TRUE if the display for this field is configurable in the given context. If TRUE, the display options returned by getDisplayOptions() may be overridden via the respective entity display.

See also

EntityDisplayInterface

array|null getDisplayOptions(string $display_context)

Returns the default display options for the field.

If the field's display is configurable, the returned display options act as default values and may be overridden via the respective entity display. Otherwise, the display options will be applied to entity displays as is.

Parameters

string $display_context

The display context. Either 'view' or 'form'.

Return Value

array|null

The array of display options for the field, or NULL if the field is not displayed. The following key/value pairs may be present:

  • label: (string) Position of the field label. The default 'field' theme implementation supports the values 'inline', 'above' and 'hidden'. Defaults to 'above'. Only applies to 'view' context.
  • region: (string) The region the field is in, or 'hidden'. If not specified, the default region will be used.
  • type: (string) The plugin (widget or formatter depending on $display_context) to use. If not specified or if the requested plugin is unknown, the 'default_widget' / 'default_formatter' for the field type will be used. Previously 'hidden' was a valid value, it is now deprecated in favor of specifying 'region' => 'hidden'.
  • settings: (array) Settings for the plugin specified above. The default settings for the plugin will be used for settings left unspecified.
  • third_party_settings: (array) Settings provided by other extensions through hook_field_formatter_third_party_settings_form().
  • weight: (float) The weight of the element. Not needed if 'type' is 'hidden'. The defaults of the various display options above get applied by the used entity display.

See also

EntityDisplayInterface

array getDefaultValueLiteral()

Returns the default value literal for the field.

This method retrieves the raw property assigned to the field definition. When computing the runtime default value for a field in a given entity, ::getDefaultValue() should be used instead.

Return Value

array

The default value for the field, as a numerically indexed array of items, each item being a property/value array (array() for no default value).

See also

FieldDefinitionInterface::getDefaultValue
FieldDefinitionInterface::getDefaultValueCallback

string|null getDefaultValueCallback()

Returns the default value callback for the field.

This method retrieves the raw property assigned to the field definition. When computing the runtime default value for a field in a given entity, ::getDefaultValue() should be used instead.

Return Value

string|null

The default value callback for the field.

See also

FieldDefinitionInterface::getDefaultValue
FieldDefinitionInterface::getDefaultValueLiteral

array getDefaultValue(FieldableEntityInterface $entity)

Returns the default value for the field in a newly created entity.

This method computes the runtime default value for a field in a given entity. To access the raw properties assigned to the field definition, ::getDefaultValueLiteral() or ::getDefaultValueCallback() should be used instead.

Parameters

FieldableEntityInterface $entity

The entity for which the default value is generated.

Return Value

array

The default value for the field, as a numerically indexed array of items, each item being a property/value array (array() for no default value).

See also

FieldDefinitionInterface::getDefaultValueLiteral
FieldDefinitionInterface::getDefaultValueCallback

bool isTranslatable()

Returns whether the field is translatable.

Return Value

bool

TRUE if the field is translatable.

FieldStorageDefinitionInterface getFieldStorageDefinition()

Returns the field storage definition.

Return Value

FieldStorageDefinitionInterface

The field storage definition.

FieldConfigInterface getConfig(string $bundle)

Gets an object that can be saved in configuration.

Base fields are defined in code. In order to configure field definition properties per bundle use this method to create an override that can be saved in configuration.

Parameters

string $bundle

The bundle to get the configurable field for.

Return Value

FieldConfigInterface

See also

BaseFieldBundleOverride

string getUniqueIdentifier()

Returns a unique identifier for the field.

Return Value

string