FieldDefinitionInterface
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
Creates a new data definition object.
Returns whether the data is multi-valued, i.e. a list of data items.
Returns whether the field can be empty.
Returns the class used for creating the typed data object.
Returns the array of settings, as required by the used class.
Returns the value of a given setting.
Returns a validation constraint.
Adds a validation constraint.
Creates a new list data definition for items of the given data type.
Gets the data definition of an item of the list.
The cache contexts associated with this object.
The cache tags associated with this object.
The maximum age for which this object may be cached.
Returns the machine name of the field.
Returns the field type.
Returns the ID of the entity type the field is attached to.
Gets the bundle the field is attached to.
Returns whether the display for the field can be configured.
Returns the default display options for the field.
Returns the default value literal for the field.
Returns the default value callback for the field.
Returns the default value for the field in a newly created entity.
Returns whether the field is translatable.
Returns the field storage definition.
Gets an object that can be saved in configuration.
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.:
string
getDataType()
Returns the data type of the data.
string|TranslatableMarkup
getLabel()
Returns a human readable label.
string|null
getDescription()
Returns a human readable description.
Descriptions are usually used on user interfaces where the data is edited or displayed.
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.
bool
isReadOnly()
Determines 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.
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.
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.
array
getSettings()
Returns the array of settings, as required by the used class.
See the documentation of the class for supported or required settings.
mixed
getSetting(string $setting_name)
Returns the value of a given setting.
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.
array
getConstraint(string $constraint_name)
Returns a validation constraint.
See \Drupal\Core\TypedData\DataDefinitionInterface::getConstraints() for details.
DataDefinitionInterface
addConstraint(string $constraint_name, array|null $options = NULL)
Adds a validation constraint.
See \Drupal\Core\TypedData\DataDefinitionInterface::getConstraints() for details.
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.
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.:
DataDefinitionInterface
getItemDefinition()
Gets the data definition of an item of the list.
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.
string[]
getCacheTags()
The cache tags associated with this object.
When this object is modified, these cache tags will be invalidated.
int
getCacheMaxAge()
The maximum age for which 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.
string
getType()
Returns the field type.
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?".
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).
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.
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.
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.
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.
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.
bool
isTranslatable()
Returns whether the field is translatable.
FieldStorageDefinitionInterface
getFieldStorageDefinition()
Returns 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.
string
getUniqueIdentifier()
Returns a unique identifier for the field.