class BaseFieldDefinition extends ListDataDefinition implements FieldDefinitionInterface, FieldStorageDefinitionInterface, RequiredFieldStorageDefinitionInterface (View source)

A class for defining entity fields.

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 string $type

The field type.

protected DataDefinitionInterface[] $propertyDefinitions

An array of field property definitions.

protected array $schema

The field schema.

protected array $indexes

Methods

$this
setTypedDataManager(TypedDataManagerInterface $typed_data_manager)

Sets the typed data manager.

getTypedDataManager()

Gets the typed data manager.

static DataDefinition
create(string $type)

Creates a new field definition.

createFromDataType($type)

Creates a new data definition object.

__construct(array $values = [], DataDefinitionInterface $item_definition = NULL)

Constructs a new data definition object.

string
getDataType()

Returns the data type of the data.

setDataType(string $type)

Sets the data type.

getLabel()

Returns a human readable label.

setLabel(string $label)

Sets the human-readable label.

string|null
getDescription()

Returns a human readable description.

setDescription(string $description)

Sets the 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.

setReadOnly(bool $read_only)

Sets whether the data is read-only.

bool
isComputed()

Determines whether the data value is computed.

setComputed(bool $computed)

Sets whether the data is computed.

bool
isRequired()

Determines whether a data value is required.

setRequired(bool $required)

Sets whether the data is required.

string
getClass()

Returns the class used for creating the typed data object.

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.

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.

setSetting(string $setting_name, mixed $value)

Sets a definition setting.

array[]
getConstraints()

Returns an array of validation constraints.

array
getConstraint(string $constraint_name)

Returns a validation constraint.

$this
setConstraints(array $constraints)

Sets an array of validation constraints.

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

Adds a validation constraint.

offsetExists($offset)

{@inheritdoc}

offsetGet($offset)

{@inheritdoc}

offsetSet($offset, $value)

{@inheritdoc}

offsetUnset($offset)

{@inheritdoc}

array
toArray()

Returns all definition values as array.

__sleep()

{@inheritdoc}

bool
isInternal()

Determines whether the data value is internal.

$this
setInternal(bool $internal)

Sets the whether the data value should be 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.

$this
setItemDefinition(DataDefinitionInterface $definition)

Sets the item definition.

__clone()

Magic method: Implements a deep clone.

static array
normalizeValue(mixed $value, string $main_property_name)

Ensure a field value is transformed into a format keyed by delta.

static $this
createFromFieldStorageDefinition(FieldStorageDefinitionInterface $definition)

Creates a new field definition based upon a field storage definition.

string
getName()

Returns the machine name of the field.

setName(string $name)

Sets the field name.

string
getType()

Returns the field type.

string
getProvider()

Returns the name of the provider of this field.

$this
setProvider(string $provider)

Sets the name of the provider of this field.

bool
isTranslatable()

Returns whether the field is translatable.

$this
setTranslatable(bool $translatable)

Sets whether the field is translatable.

bool
isRevisionable()

Returns whether the field storage is revisionable.

$this
setRevisionable(bool $revisionable)

Sets whether the field is revisionable.

int
getCardinality()

Returns the maximum number of items allowed for the field.

$this
setCardinality(int $cardinality)

Sets the maximum number of items allowed for the field.

bool
isMultiple()

Returns whether the field can contain multiple items.

setPropertyConstraints(string $name, array $constraints)

Sets constraints for a given field item property.

addPropertyConstraints(string $name, array $constraints)

Adds constraints for a given field item property.

setDisplayOptions(string $display_context, array $options)

Sets the display options for the field in forms or rendered entities.

setDisplayConfigurable(string $display_context, bool $configurable)

Sets whether the display for the field can be configured.

array|null
getDisplayOptions(string $display_context)

Returns the default display options for the field.

bool
isDisplayConfigurable(string $display_context)

Returns whether the display for the field can be configured.

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.

setDefaultValue($value)

{@inheritdoc}

setDefaultValueCallback($callback)

{@inheritdoc}

array
getInitialValue()

Returns the initial value for the field.

$this
setInitialValue(mixed $value)

Sets an initial value for the field.

string|null
getInitialValueFromField()

Returns the name of the field that will be used for getting initial values.

$this
setInitialValueFromField(string $field_name, mixed $default_value = NULL)

Sets a field that will be used for getting initial values.

getOptionsProvider(string $property_name, FieldableEntityInterface $entity)

Gets an options provider for the given field item property.

getPropertyDefinition(string $name)

Gets the definition of a contained property.

getPropertyDefinitions()

Gets an array of property definitions of contained properties.

string[]
getPropertyNames()

Returns the names of the field's subproperties.

string|null
getMainPropertyName()

Returns the name of the main property, if any.

string
getTargetEntityTypeId()

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

$this
setTargetEntityTypeId(string $entity_type_id)

Sets the ID of the type of the entity this field is attached to.

string|null
getTargetBundle()

Gets the bundle the field is attached to.

$this
setTargetBundle(string|null $bundle)

Sets the bundle this field is defined for.

array[]
getSchema()

Returns the field schema.

array[]
getColumns()

Returns the field columns, as defined in the field schema.

bool
hasCustomStorage()

Returns the storage behavior for this field.

bool
isBaseField()

Determines whether the field is a base field.

$this
setCustomStorage(bool $custom_storage)

Sets the storage behavior for this field.

getFieldStorageDefinition()

Returns the field storage definition.

string
getUniqueStorageIdentifier()

Returns a unique identifier for the field storage.

string
getUniqueIdentifier()

Returns a unique identifier for the field.

bool
isDeleted()

Returns whether the field is deleted or not.

$this
setDeleted(bool $deleted)

Sets whether the field storage is deleted.

getConfig(string $bundle)

Gets an object that can be saved in configuration.

bool
isStorageRequired()

Returns whether the field storage is required.

setStorageRequired(bool $required)

Sets whether the field storage is required.

Details

$this setTypedDataManager(TypedDataManagerInterface $typed_data_manager)

Sets the typed data manager.

Parameters

TypedDataManagerInterface $typed_data_manager

The typed data manager.

Return Value

$this

TypedDataManagerInterface getTypedDataManager()

Gets the typed data manager.

Return Value

TypedDataManagerInterface

The typed data manager.

static DataDefinition create(string $type)

Creates a new field definition.

Parameters

string $type

The type of the field.

Return Value

DataDefinition

A new DataDefinition object.

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

Parameters

$type

Return Value

DataDefinitionInterface

Exceptions

InvalidArgumentException

__construct(array $values = [], DataDefinitionInterface $item_definition = NULL)

Constructs a new data definition object.

Parameters

array $values

(optional) If given, an array of initial values to set on the definition.

DataDefinitionInterface $item_definition

string getDataType()

Returns the data type of the data.

Return Value

string

The data type.

DataDefinition setDataType(string $type)

Sets the data type.

Parameters

string $type

The data type to set.

Return Value

DataDefinition

The object itself for chaining.

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.

DataDefinition setLabel(string $label)

Sets the human-readable label.

Parameters

string $label

The label to set.

Return Value

DataDefinition

The object itself for chaining.

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.

DataDefinition setDescription(string $description)

Sets the human-readable description.

Parameters

string $description

The description to set.

Return Value

DataDefinition

The object itself for chaining.

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.

DataDefinition setReadOnly(bool $read_only)

Sets whether the data is read-only.

Parameters

bool $read_only

Whether the data is read-only.

Return Value

DataDefinition

The object itself for chaining.

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.

DataDefinition setComputed(bool $computed)

Sets whether the data is computed.

Parameters

bool $computed

Whether the data is computed.

Return Value

DataDefinition

The object itself for chaining.

bool isRequired()

Determines whether a data value is required.

For required data a non-NULL value is mandatory.

Return Value

bool

Whether a data value is required.

DataDefinition setRequired(bool $required)

Sets whether the data is required.

Parameters

bool $required

Whether the data is required.

Return Value

DataDefinition

The object itself for chaining.

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.

DataDefinition setClass(string|null $class)

Sets the class used for creating the typed data object.

Parameters

string|null $class

The class to use.

Return Value

DataDefinition

The object itself for chaining.

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.

DataDefinition setSettings(array $settings)

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

Parameters

array $settings

The array of settings.

Return Value

DataDefinition

The object itself for chaining.

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.

DataDefinition setSetting(string $setting_name, mixed $value)

Sets a definition setting.

Parameters

string $setting_name

The definition setting to set.

mixed $value

The value to set.

Return Value

DataDefinition

The object itself for chaining.

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.

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.

$this setConstraints(array $constraints)

Sets an array of validation constraints.

Parameters

array $constraints

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

Return Value

$this

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.

offsetExists($offset)

{@inheritdoc}

This is for BC support only.

Parameters

$offset

offsetGet($offset)

{@inheritdoc}

This is for BC support only.

Parameters

$offset

offsetSet($offset, $value)

{@inheritdoc}

This is for BC support only.

Parameters

$offset
$value

offsetUnset($offset)

{@inheritdoc}

This is for BC support only.

Parameters

$offset

array toArray()

Returns all definition values as array.

Return Value

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.

Return Value

bool

Whether the data value is internal.

$this setInternal(bool $internal)

Sets the whether the data value should be internal.

Parameters

bool $internal

Whether the data value should be internal.

Return Value

$this

See also

DataDefinitionInterface::isInternal

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.

$this setItemDefinition(DataDefinitionInterface $definition)

Sets the item definition.

Parameters

DataDefinitionInterface $definition

A list item's data definition.

Return Value

$this

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

Parameters

mixed $value

The raw field value to normalize.

string $main_property_name

The main field property name.

Return Value

array

A field value normalized into a format keyed by delta.

static $this createFromFieldStorageDefinition(FieldStorageDefinitionInterface $definition)

Creates a new field definition based upon a field storage definition.

In cases where one needs a field storage definitions to act like full field definitions, this creates a new field definition based upon the (limited) information available. That way it is possible to use the field definition in places where a full field definition is required; e.g., with widgets or formatters.

Parameters

FieldStorageDefinitionInterface $definition

The field storage definition to base the new field definition upon.

Return Value

$this

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.

BaseFieldDefinition setName(string $name)

Sets the field name.

Parameters

string $name

The field name to set.

Return Value

BaseFieldDefinition

The object itself for chaining.

string getType()

Returns the field type.

Return Value

string

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

string getProvider()

Returns the name of the provider of this field.

Return Value

string

The provider name; e.g., the module name.

$this setProvider(string $provider)

Sets the name of the provider of this field.

Parameters

string $provider

The provider name to set.

Return Value

$this

bool isTranslatable()

Returns whether the field is translatable.

Return Value

bool

TRUE if the field is translatable.

$this setTranslatable(bool $translatable)

Sets whether the field is translatable.

Parameters

bool $translatable

Whether the field supports translation.

Return Value

$this

bool isRevisionable()

Returns whether the field storage is revisionable.

Note that if the entity type is revisionable and the field storage has a cardinality higher than 1, the field storage is considered revisionable by default.

Return Value

bool

TRUE if the field is revisionable.

$this setRevisionable(bool $revisionable)

Sets whether the field is revisionable.

Parameters

bool $revisionable

Whether the field is revisionable.

Return Value

$this

The object itself for chaining.

int getCardinality()

Returns the maximum number of items allowed for the field.

Possible values are positive integers or FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED.

Return Value

int

The field cardinality.

$this setCardinality(int $cardinality)

Sets the maximum number of items allowed for the field.

Possible values are positive integers or FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED.

Note that if the entity type that this base field is attached to is revisionable and the field has a cardinality higher than 1, the field is considered revisionable by default.

Parameters

int $cardinality

The field cardinality.

Return Value

$this

bool isMultiple()

Returns whether the field can contain multiple items.

Return Value

bool

TRUE if the field can contain multiple items, FALSE otherwise.

BaseFieldDefinition setPropertyConstraints(string $name, array $constraints)

Sets constraints for a given field item property.

Note: this overwrites any existing property constraints. If you need to add to the existing constraints, use \Drupal\Core\Field\BaseFieldDefinition::addPropertyConstraints()

Parameters

string $name

The name of the property to set constraints for.

array $constraints

The constraints to set.

Return Value

BaseFieldDefinition

The object itself for chaining.

BaseFieldDefinition addPropertyConstraints(string $name, array $constraints)

Adds constraints for a given field item property.

Adds a constraint to a property of a base field item. e.g.

Parameters

string $name

The name of the property to set constraints for.

array $constraints

The constraints to set.

Return Value

BaseFieldDefinition

The object itself for chaining.

See also

\Drupal\Core\Field\BaseFieldDefinition::addConstraint()

BaseFieldDefinition 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).

Parameters

string $display_context

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

array $options

An array of display options. Refer to \Drupal\Core\Field\FieldDefinitionInterface::getDisplayOptions() for a list of supported keys. The options should include at least a 'weight', or specify 'type' = 'hidden'. The 'default_widget' / 'default_formatter' for the field type will be used if no 'type' is specified.

Return Value

BaseFieldDefinition

The object itself for chaining.

BaseFieldDefinition setDisplayConfigurable(string $display_context, bool $configurable)

Sets whether the display for the field can be configured.

Parameters

string $display_context

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

bool $configurable

Whether the display options can be configured (e.g., via the "Manage display" / "Manage form display" UI screens). If TRUE, the options specified via getDisplayOptions() act as defaults.

Return Value

BaseFieldDefinition

The object itself for chaining.

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.

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.

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

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.

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

setDefaultValue($value)

{@inheritdoc}

Parameters

$value

setDefaultValueCallback($callback)

{@inheritdoc}

Parameters

$callback

array getInitialValue()

Returns the initial value for the field.

Return Value

array

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

$this setInitialValue(mixed $value)

Sets an initial value for the field.

Parameters

mixed $value

The initial value for the field. This can be either:

  • a literal, in which case it will be assigned to the first property of the first item;
  • a numerically indexed array of items, each item being a property/value array;
  • a non-numerically indexed array, in which case the array is assumed to be a property/value array and used as the first item;
  • an empty array for no initial value.

Return Value

$this

string|null getInitialValueFromField()

Returns the name of the field that will be used for getting initial values.

Return Value

string|null

The field name.

$this setInitialValueFromField(string $field_name, mixed $default_value = NULL)

Sets a field that will be used for getting initial values.

Parameters

string $field_name

The name of the field that will be used for getting initial values.

mixed $default_value

(optional) The default value for the field, in case the inherited value is NULL. This can be either:

  • a literal, in which case it will be assigned to the first property of the first item;
  • a numerically indexed array of items, each item being a property/value array;
  • a non-numerically indexed array, in which case the array is assumed to be a property/value array and used as the first item;
  • an empty array for no initial value. If the field being added is required or an entity key, it is recommended to provide a default value.

Return Value

$this

OptionsProviderInterface|null getOptionsProvider(string $property_name, FieldableEntityInterface $entity)

Gets an options provider for the given field item property.

Parameters

string $property_name

The name of the property to get options for; e.g., 'value'.

FieldableEntityInterface $entity

The entity for which the options should be provided.

Return Value

OptionsProviderInterface|null

An options provider, or NULL if no options are defined.

DataDefinitionInterface|null getPropertyDefinition(string $name)

Gets the definition of a contained property.

Parameters

string $name

The name of property.

Return Value

DataDefinitionInterface|null

The definition of the property or NULL if the property does not exist.

DataDefinitionInterface[] getPropertyDefinitions()

Gets an array of property definitions of contained properties.

Return Value

DataDefinitionInterface[]

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

string[] getPropertyNames()

Returns the names of the field's subproperties.

A field is a list of items, and each item can contain one or more properties. All items for a given field contain the same property names, but the values can be different for each item.

For example, an email field might just contain a single 'value' property, while a link field might contain 'title' and 'url' properties, and a text field might contain 'value', 'summary', and 'format' properties.

Return Value

string[]

The property names.

string|null getMainPropertyName()

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 @code target_id @endcode 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.

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.

$this setTargetEntityTypeId(string $entity_type_id)

Sets the ID of the type of the entity this field is attached to.

Parameters

string $entity_type_id

The name of the target entity type to set.

Return Value

$this

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.

$this setTargetBundle(string|null $bundle)

Sets the bundle this field is defined for.

Parameters

string|null $bundle

The bundle, or NULL if the field is not bundle-specific.

Return Value

$this

array[] getSchema()

Returns the field schema.

Note that this method returns an empty array for computed fields which have no schema.

Return Value

array[]

The field schema, as an array of key/value pairs in the format returned by \Drupal\Core\Field\FieldItemInterface::schema():

  • columns: An array of Schema API column specifications, keyed by column name. This specifies what comprises a single value for a given field. No assumptions should be made on how storage backends internally use the original column name to structure their storage.
  • indexes: An array of Schema API index definitions. Some storage backends might not support indexes.
  • unique keys: An array of Schema API unique key definitions. Some storage backends might not support unique keys.
  • foreign keys: An array of Schema API foreign key definitions. Note, however, that depending on the storage backend specified for the field, the field data is not necessarily stored in SQL.

array[] getColumns()

Returns the field columns, as defined in the field schema.

Return Value

array[]

The array of field columns, keyed by column name, in the same format returned by getSchema().

bool hasCustomStorage()

Returns the storage behavior for this field.

Indicates whether the entity type's storage should take care of storing the field values or whether it is handled separately; e.g. by the module providing the field.

Return Value

bool

FALSE if the storage takes care of storing the field, TRUE otherwise.

bool isBaseField()

Determines whether the field is a base field.

Base fields are not specific to a given bundle or a set of bundles. This excludes configurable fields, as they are always attached to a specific bundle.

Return Value

bool

Whether the field is a base field.

$this setCustomStorage(bool $custom_storage)

Sets the storage behavior for this field.

Parameters

bool $custom_storage

Pass FALSE if the storage takes care of storing the field, TRUE otherwise.

Return Value

$this

Exceptions

LogicException

FieldStorageDefinitionInterface getFieldStorageDefinition()

Returns the field storage definition.

Return Value

FieldStorageDefinitionInterface

The field storage definition.

string getUniqueStorageIdentifier()

Returns a unique identifier for the field storage.

Return Value

string

string getUniqueIdentifier()

Returns a unique identifier for the field.

Return Value

string

bool isDeleted()

Returns whether the field is deleted or not.

Return Value

bool

TRUE if the field is deleted, FALSE otherwise.

$this setDeleted(bool $deleted)

Sets whether the field storage is deleted.

Parameters

bool $deleted

Whether the field storage is deleted.

Return Value

$this

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

bool isStorageRequired()

Returns whether the field storage is required.

If a field storage is required, NOT NULL constraints will be added automatically for the required properties of a field type.

Return Value

bool

TRUE if the field storage is required, FALSE otherwise.

BaseFieldDefinition setStorageRequired(bool $required)

Sets whether the field storage is required.

Parameters

bool $required

Whether the field storage is required.

Return Value

BaseFieldDefinition

The object itself for chaining.