class DataDefinition
Same name in other branches
- 8.9.x core/lib/Drupal/Core/TypedData/DataDefinition.php \Drupal\Core\TypedData\DataDefinition
- 10 core/lib/Drupal/Core/TypedData/DataDefinition.php \Drupal\Core\TypedData\DataDefinition
- 11.x core/lib/Drupal/Core/TypedData/DataDefinition.php \Drupal\Core\TypedData\DataDefinition
A typed data definition class for defining data based on defined data types.
Hierarchy
- class \Drupal\Core\TypedData\DataDefinition implements \Drupal\Core\TypedData\DataDefinitionInterface, \Drupal\Core\TypedData\ArrayAccess uses \Drupal\Core\TypedData\TypedDataTrait
Expanded class hierarchy of DataDefinition
55 files declare their use of DataDefinition
- AllowedValuesConstraintValidatorTest.php in core/
tests/ Drupal/ KernelTests/ Core/ TypedData/ AllowedValuesConstraintValidatorTest.php - BaseFieldDefinitionTest.php in core/
tests/ Drupal/ Tests/ Core/ Entity/ BaseFieldDefinitionTest.php - BooleanItem.php in core/
lib/ Drupal/ Core/ Field/ Plugin/ Field/ FieldType/ BooleanItem.php - BundleConstraintValidatorTest.php in core/
tests/ Drupal/ KernelTests/ Core/ Entity/ BundleConstraintValidatorTest.php - CommentItem.php in core/
modules/ comment/ src/ Plugin/ Field/ FieldType/ CommentItem.php
File
-
core/
lib/ Drupal/ Core/ TypedData/ DataDefinition.php, line 8
Namespace
Drupal\Core\TypedDataView source
class DataDefinition implements DataDefinitionInterface, \ArrayAccess {
use TypedDataTrait;
/**
* The array holding values for all definition keys.
*
* @var array
*/
protected $definition = [];
/**
* Creates a new data definition.
*
* @param string $type
* The data type of the data; e.g., 'string', 'integer' or 'any'.
*
* @return static
* A new DataDefinition object.
*/
public static function create($type) {
$definition['type'] = $type;
return new static($definition);
}
/**
* {@inheritdoc}
*/
public static function createFromDataType($type) {
return self::create($type);
}
/**
* Constructs a new data definition object.
*
* @param array $values
* (optional) If given, an array of initial values to set on the definition.
*/
public function __construct(array $values = []) {
$this->definition = $values;
}
/**
* {@inheritdoc}
*/
public function getDataType() {
return !empty($this->definition['type']) ? $this->definition['type'] : 'any';
}
/**
* Sets the data type.
*
* @param string $type
* The data type to set.
*
* @return static
* The object itself for chaining.
*/
public function setDataType($type) {
$this->definition['type'] = $type;
return $this;
}
/**
* {@inheritdoc}
*/
public function getLabel() {
return $this->definition['label'] ?? NULL;
}
/**
* Sets the human-readable label.
*
* @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $label
* The label to set.
*
* @return static
* The object itself for chaining.
*/
public function setLabel($label) {
$this->definition['label'] = $label;
return $this;
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->definition['description'] ?? NULL;
}
/**
* Sets the human-readable description.
*
* @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $description
* The description to set.
*
* @return static
* The object itself for chaining.
*/
public function setDescription($description) {
$this->definition['description'] = $description;
return $this;
}
/**
* {@inheritdoc}
*/
public function isList() {
return $this instanceof ListDataDefinitionInterface;
}
/**
* {@inheritdoc}
*/
public function isReadOnly() {
if (!isset($this->definition['read-only'])) {
// Default to read-only if the data value is computed.
return $this->isComputed();
}
return !empty($this->definition['read-only']);
}
/**
* Sets whether the data is read-only.
*
* @param bool $read_only
* Whether the data is read-only.
*
* @return static
* The object itself for chaining.
*/
public function setReadOnly($read_only) {
$this->definition['read-only'] = $read_only;
return $this;
}
/**
* {@inheritdoc}
*/
public function isComputed() {
return !empty($this->definition['computed']);
}
/**
* Sets whether the data is computed.
*
* @param bool $computed
* Whether the data is computed.
*
* @return static
* The object itself for chaining.
*/
public function setComputed($computed) {
$this->definition['computed'] = $computed;
return $this;
}
/**
* {@inheritdoc}
*/
public function isRequired() {
return !empty($this->definition['required']);
}
/**
* Sets whether the data is required.
*
* @param bool $required
* Whether the data is required.
*
* @return static
* The object itself for chaining.
*/
public function setRequired($required) {
$this->definition['required'] = $required;
return $this;
}
/**
* {@inheritdoc}
*/
public function getClass() {
if (isset($this->definition['class'])) {
return $this->definition['class'];
}
else {
$type_definition = \Drupal::typedDataManager()->getDefinition($this->getDataType());
return $type_definition['class'];
}
}
/**
* Sets the class used for creating the typed data object.
*
* @param string|null $class
* The class to use.
*
* @return static
* The object itself for chaining.
*/
public function setClass($class) {
$this->definition['class'] = $class;
return $this;
}
/**
* {@inheritdoc}
*/
public function getSettings() {
return $this->definition['settings'] ?? [];
}
/**
* Sets the array of settings, as required by the used class.
*
* @param array $settings
* The array of settings.
*
* @return static
* The object itself for chaining.
*/
public function setSettings(array $settings) {
$this->definition['settings'] = $settings;
return $this;
}
/**
* {@inheritdoc}
*/
public function getSetting($setting_name) {
return $this->definition['settings'][$setting_name] ?? NULL;
}
/**
* Sets a definition setting.
*
* @param string $setting_name
* The definition setting to set.
* @param mixed $value
* The value to set.
*
* @return static
* The object itself for chaining.
*/
public function setSetting($setting_name, $value) {
$this->definition['settings'][$setting_name] = $value;
return $this;
}
/**
* {@inheritdoc}
*/
public function getConstraints() {
$constraints = $this->definition['constraints'] ?? [];
$constraints += $this->getTypedDataManager()
->getDefaultConstraints($this);
return $constraints;
}
/**
* {@inheritdoc}
*/
public function getConstraint($constraint_name) {
$constraints = $this->getConstraints();
return $constraints[$constraint_name] ?? NULL;
}
/**
* Sets an array of validation constraints.
*
* @param 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 $this
*/
public function setConstraints(array $constraints) {
$this->definition['constraints'] = $constraints;
return $this;
}
/**
* {@inheritdoc}
*/
public function addConstraint($constraint_name, $options = NULL) {
$this->definition['constraints'][$constraint_name] = $options;
return $this;
}
/**
* {@inheritdoc}
*
* This is for BC support only.
* @todo: Remove in https://www.drupal.org/node/1928868.
*/
public function offsetExists($offset) {
// PHP's array access does not work correctly with isset(), so we have to
// bake isset() in here. See https://bugs.php.net/bug.php?id=41727.
return array_key_exists($offset, $this->definition) && isset($this->definition[$offset]);
}
/**
* {@inheritdoc}
*
* This is for BC support only.
* @todo: Remove in https://www.drupal.org/node/1928868.
*/
public function &offsetGet($offset) {
if (!isset($this->definition[$offset])) {
$this->definition[$offset] = NULL;
}
return $this->definition[$offset];
}
/**
* {@inheritdoc}
*
* This is for BC support only.
* @todo: Remove in https://www.drupal.org/node/1928868.
*/
public function offsetSet($offset, $value) {
$this->definition[$offset] = $value;
}
/**
* {@inheritdoc}
*
* This is for BC support only.
* @todo: Remove in https://www.drupal.org/node/1928868.
*/
public function offsetUnset($offset) {
unset($this->definition[$offset]);
}
/**
* Returns all definition values as array.
*
* @return array
*/
public function toArray() {
return $this->definition;
}
/**
* {@inheritdoc}
*/
public function __sleep() {
// Never serialize the typed data manager.
$vars = get_object_vars($this);
unset($vars['typedDataManager']);
return array_keys($vars);
}
/**
* {@inheritdoc}
*/
public function isInternal() {
// Respect the definition, otherwise default to TRUE for computed fields.
if (isset($this->definition['internal'])) {
return $this->definition['internal'];
}
return $this->isComputed();
}
/**
* Sets the whether the data value should be internal.
*
* @param bool $internal
* Whether the data value should be internal.
*
* @return $this
*
* @see \Drupal\Core\TypedData\DataDefinitionInterface::isInternal
*/
public function setInternal($internal) {
$this->definition['internal'] = $internal;
return $this;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
DataDefinition::$definition | protected | property | The array holding values for all definition keys. | ||
DataDefinition::addConstraint | public | function | Adds a validation constraint. | Overrides DataDefinitionInterface::addConstraint | |
DataDefinition::create | public static | function | Creates a new data definition. | 5 | |
DataDefinition::createFromDataType | public static | function | Creates a new data definition object. | Overrides DataDefinitionInterface::createFromDataType | 5 |
DataDefinition::getClass | public | function | Returns the class used for creating the typed data object. | Overrides DataDefinitionInterface::getClass | 1 |
DataDefinition::getConstraint | public | function | Returns a validation constraint. | Overrides DataDefinitionInterface::getConstraint | |
DataDefinition::getConstraints | public | function | Returns an array of validation constraints. | Overrides DataDefinitionInterface::getConstraints | 1 |
DataDefinition::getDataType | public | function | Returns the data type of the data. | Overrides DataDefinitionInterface::getDataType | 2 |
DataDefinition::getDescription | public | function | Returns a human readable description. | Overrides DataDefinitionInterface::getDescription | |
DataDefinition::getLabel | public | function | Returns a human readable label. | Overrides DataDefinitionInterface::getLabel | |
DataDefinition::getSetting | public | function | Returns the value of a given setting. | Overrides DataDefinitionInterface::getSetting | 2 |
DataDefinition::getSettings | public | function | Returns the array of settings, as required by the used class. | Overrides DataDefinitionInterface::getSettings | 2 |
DataDefinition::isComputed | public | function | Determines whether the data value is computed. | Overrides DataDefinitionInterface::isComputed | |
DataDefinition::isInternal | public | function | Determines whether the data value is internal. | Overrides DataDefinitionInterface::isInternal | 1 |
DataDefinition::isList | public | function | Returns whether the data is multi-valued, i.e. a list of data items. | Overrides DataDefinitionInterface::isList | |
DataDefinition::isReadOnly | public | function | Determines whether the data is read-only. | Overrides DataDefinitionInterface::isReadOnly | |
DataDefinition::isRequired | public | function | Determines whether a data value is required. | Overrides DataDefinitionInterface::isRequired | |
DataDefinition::offsetExists | public | function | This is for BC support only. @todo: Remove in https://www.drupal.org/node/1928868. |
||
DataDefinition::offsetGet | public | function | This is for BC support only. @todo: Remove in https://www.drupal.org/node/1928868. |
||
DataDefinition::offsetSet | public | function | This is for BC support only. @todo: Remove in https://www.drupal.org/node/1928868. |
||
DataDefinition::offsetUnset | public | function | This is for BC support only. @todo: Remove in https://www.drupal.org/node/1928868. |
||
DataDefinition::setClass | public | function | Sets the class used for creating the typed data object. | ||
DataDefinition::setComputed | public | function | Sets whether the data is computed. | ||
DataDefinition::setConstraints | public | function | Sets an array of validation constraints. | ||
DataDefinition::setDataType | public | function | Sets the data type. | 1 | |
DataDefinition::setDescription | public | function | Sets the human-readable description. | ||
DataDefinition::setInternal | public | function | Sets the whether the data value should be internal. | ||
DataDefinition::setLabel | public | function | Sets the human-readable label. | ||
DataDefinition::setReadOnly | public | function | Sets whether the data is read-only. | ||
DataDefinition::setRequired | public | function | Sets whether the data is required. | ||
DataDefinition::setSetting | public | function | Sets a definition setting. | 2 | |
DataDefinition::setSettings | public | function | Sets the array of settings, as required by the used class. | 2 | |
DataDefinition::toArray | public | function | Returns all definition values as array. | ||
DataDefinition::__construct | public | function | Constructs a new data definition object. | 1 | |
DataDefinition::__sleep | public | function | 2 | ||
TypedDataTrait::$typedDataManager | protected | property | The typed data manager used for creating the data types. | ||
TypedDataTrait::getTypedDataManager | public | function | Gets the typed data manager. | 2 | |
TypedDataTrait::setTypedDataManager | public | function | Sets the typed data manager. | 2 |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.