class DataReferenceTargetDefinition extends DataDefinition (View source)

A typed data definition class for the entity reference target_id property.

The target_id property differs from other data definitions in that it is required at the storage level, but not at the validation level. This is because its value can be set just-in-time using the preSave() method.

Validation for the target_id property is provided by the 'ValidReference' validation constraint.

Traits

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

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 data definition.

createFromDataType($type)

Creates a new data definition object.

__construct(array $values = [])

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.

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 data definition.

Parameters

string $type

The data type of the data; e.g., 'string', 'integer' or 'any'.

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 = [])

Constructs a new data definition object.

Parameters

array $values

(optional) If given, an array of initial values to set on the 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