FieldDefinition
class FieldDefinition extends ListDataDefinition implements FieldDefinitionInterface (View source)
A class for defining entity field definitions.
A field definition in the context of a bundle field is different from a base field in that it may exist only for one or more bundles of an entity type. A bundle field definition may also override the definition of an existing base field definition on a per bundle basis. The bundle field definition is used for code driven overrides, while the \Drupal\Core\Field\Entity\BaseFieldOverride uses config to override the base field definition.
Bundle fields can be defined in code using hook_entity_bundle_field_info() or via the \Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions() method when defining an entity type. All bundle fields require an associated storage definition. A storage definition may have automatically been defined when overriding a base field or it may be manually provided via hook_entity_field_storage_info().
Traits
Trait to implement CacheableDependencyInterface for unchanging objects.
A trait used to assist in the normalization of raw input field values.
Wrapper methods for classes that needs typed data manager object.
Properties
| protected TypedDataManagerInterface | $typedDataManager | The typed data manager used for creating the data types. |
from TypedDataTrait |
| protected array | $definition | The array holding values for all definition keys. |
from DataDefinition |
| protected DataDefinitionInterface | $itemDefinition | The data definition of a list item. |
from ListDataDefinition |
| protected FieldStorageDefinitionInterface | $fieldStorageDefinition | The associated field storage definition. |
Methods
Creates a new list definition.
Creates a new data definition object.
Constructs a new data definition object.
Sets the human-readable description.
Returns whether the data is multi-valued, i.e. a list of data items.
Returns the class used for creating the typed data object.
Sets the class used for creating the typed data object.
Returns the array of settings, as required by the used class.
Sets the array of settings, as required by the used class.
Returns the value of a given setting.
Sets a definition setting.
Sets an array of validation constraints.
Adds a validation constraint.
Sets the whether the data value should be internal.
Creates a new list data definition for items of the given data type.
Gets the data definition of an item of the list.
Ensure a field value is transformed into a format keyed by delta.
Creates a new field definition.
Returns the machine name of the field.
Returns the field type.
Returns the ID of the entity type the field is attached to.
Set the target bundle.
Gets the bundle the field is attached to.
Sets whether the display for the field can be configured.
Returns whether the display for the field can be configured.
Sets the display options for the field in forms or rendered entities.
Returns the default display options for the field.
Returns the default value literal for the field.
Set the default value callback for the field.
Returns the default value callback for the field.
Set a default value for the field.
Returns the default value for the field in a newly created entity.
Sets whether the field is translatable.
Returns whether the field is translatable.
Set the field storage definition.
Returns the field storage definition.
Gets an object that can be saved in configuration.
Returns a unique identifier for the field.
Details
$this
setTypedDataManager(TypedDataManagerInterface $typed_data_manager)
Sets the typed data manager.
TypedDataManagerInterface
getTypedDataManager()
Gets the typed data manager.
static DataDefinition
create(string $item_type)
Creates a new list definition.
static DataDefinitionInterface
createFromDataType($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.:
__construct(array $values = [], DataDefinitionInterface $item_definition = NULL)
Constructs a new data definition object.
string
getDataType()
Returns the data type of the data.
DataDefinition
setDataType(string $type)
Sets the data type.
string|TranslatableMarkup
getLabel()
Returns a human readable label.
DataDefinition
setLabel(string $label)
Sets the 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.
DataDefinition
setDescription(string $description)
Sets the human-readable description.
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.
DataDefinition
setReadOnly(bool $read_only)
Sets 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.
DataDefinition
setComputed(bool $computed)
Sets whether the data is computed.
bool
isRequired()
Determines whether a data value is required.
For required data a non-NULL value is mandatory.
DataDefinition
setRequired(bool $required)
Sets whether the data is required.
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.
DataDefinition
setClass(string|null $class)
Sets 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.
DataDefinition
setSettings(array $settings)
Sets the array of settings, as required by the used class.
mixed
getSetting(string $setting_name)
Returns the value of a given setting.
DataDefinition
setSetting(string $setting_name, mixed $value)
Sets a definition 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.
$this
setConstraints(array $constraints)
Sets an array of validation constraints.
DataDefinitionInterface
addConstraint(string $constraint_name, array|null $options = NULL)
Adds a validation constraint.
See \Drupal\Core\TypedData\DataDefinitionInterface::getConstraints() for details.
offsetExists($offset)
{@inheritdoc}
This is for BC support only.
offsetGet($offset)
{@inheritdoc}
This is for BC support only.
offsetSet($offset, $value)
{@inheritdoc}
This is for BC support only.
offsetUnset($offset)
{@inheritdoc}
This is for BC support only.
array
toArray()
Returns all definition values as array.
__sleep()
{@inheritdoc}
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.
$this
setInternal(bool $internal)
Sets the whether the data value should be 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.:
DataDefinitionInterface
getItemDefinition()
Gets the data definition of an item of the list.
$this
setItemDefinition(DataDefinitionInterface $definition)
Sets the item definition.
__clone()
Magic method: Implements a deep clone.
getCacheContexts()
{@inheritdoc}
getCacheTags()
{@inheritdoc}
getCacheMaxAge()
{@inheritdoc}
static protected array
normalizeValue(mixed $value, string $main_property_name)
Ensure a field value is transformed into a format keyed by delta.
static FieldDefinition
createFromFieldStorageDefinition(FieldStorageDefinitionInterface $storageDefinition)
Creates a new field definition.
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?".
$this
setTargetBundle(string $bundle)
Set the target bundle.
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).
$this
setDisplayConfigurable(string $display_context, bool $configurable)
Sets whether the display for the field can be configured.
bool
isDisplayConfigurable(string $display_context)
Returns whether the display for the field can be configured.
$this
setDisplayOptions(string $display_context, array $options)
Sets the display options for the field in forms or rendered entities.
This enables generic rendering of the field with widgets / formatters, including automated support for "In place editing", and with optional configurability in the "Manage display" / "Manage form display" UI screens.
Unless this method is called, the field remains invisible (or requires ad-hoc rendering logic).
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.
$this
setDefaultValueCallback(string $callback)
Set the default value callback for the field.
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.
$this
setDefaultValue(mixed $value)
Set a default value for the field.
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.
$this
setTranslatable(bool $translatable)
Sets whether the field is translatable.
bool
isTranslatable()
Returns whether the field is translatable.
$this
setFieldStorageDefinition(FieldStorageDefinitionInterface $storageDefinition)
Set the field storage definition.
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.