API for describing data based on a set of available data types.
PHP has data types, such as int, string, float, array, etc., and it is an object-oriented language that lets you define classes and interfaces. However, in some cases, it is useful to be able to define an abstract type (as in an interface, free of implementation details), that still has properties (which an interface cannot) as well as meta-data. The Typed Data API provides this abstraction.
Each data type in the Typed Data API is a plugin class (annotation class example: \Drupal\Core\TypedData\Annotation\DataType); these plugins are managed by the typed_data_manager service (by default \Drupal\Core\TypedData\TypedDataManager). Each data object encapsulates a single piece of data, provides access to the metadata, and provides validation capability. Also, the typed data plugins have a shorthand for easily accessing data values, described in Tree handling.
The metadata of a data object is defined by an object based on a class called the definition class (see \Drupal\Core\TypedData\DataDefinitionInterface). The class used can vary by data type and can be specified in the data type's plugin definition, while the default is set in the $definition_class property of the annotation class. The default class is \Drupal\Core\TypedData\DataDefinition. For data types provided by a plugin deriver, the plugin deriver can set the definition_class property too. The metadata object provides information about the data, such as the data type, whether it is translatable, the names of its properties (for complex types), and who can access it.
See https://www.drupal.org/node/1794140 for more information about the Typed Data API.
There are three kinds of typed data: primitive, complex, and list.
Primitive data types wrap PHP data types and also serve as building blocks for complex and list typed data. Each primitive data type has an interface that extends \Drupal\Core\TypedData\PrimitiveInterface, with getValue() and setValue() methods for accessing the data value, and a default plugin implementation. Here's a list:
Complex data types, with interface \Drupal\Core\TypedData\ComplexDataInterface, represent data with named properties; the properties can be accessed with get() and set() methods. The value of each property is itself a typed data object, which can be primitive, complex, or list data.
The base type for most complex data is the \Drupal\Core\TypedData\Plugin\DataType\Map class, which represents an associative array. Map provides its own definition class in the annotation, \Drupal\Core\TypedData\MapDataDefinition, and most complex data classes extend this class. The getValue() and setValue() methods on the Map class enforce the data definition and its property structure.
The Drupal Field API uses complex typed data for its field items, with definition class \Drupal\Core\Field\TypedData\FieldItemDataDefinition.
List data types, with interface \Drupal\Core\TypedData\ListInterface, represent data that is an ordered list of typed data, all of the same type. More precisely, the plugins in the list must have the same base plugin ID; however, some types (for example field items and entities) are provided by plugin derivatives and the sub IDs can be different.
Typed data allows you to use shorthand to get data values nested in the implicit tree structure of the data. For example, to get the value from an entity field item, the Entity Field API allows you to call:
$value = $entity->fieldName->propertyName;
This is really shorthand for:
$field_item_list = $entity->get('fieldName'); $field_item = $field_item_list->get(0); $property = $field_item->get('propertyName'); $value = $property->getValue();
Some notes:
To define a new data type:
The data types of the Typed Data API can be used in several ways, once they have been defined:
Services and Dependency Injection Container
Name | Location | Description |
---|---|---|
DataType | core/lib/Drupal/Core/TypedData/Annotation/DataType.php | Defines a data type annotation object. |
ItemList | core/lib/Drupal/Core/TypedData/Plugin/DataType/ItemList.php | A generic list class. |
Map | core/lib/Drupal/Core/TypedData/Plugin/DataType/Map.php | The "map" data type. |
TypedData | core/lib/Drupal/Core/TypedData/TypedData.php | The abstract base class for typed data. |
Name | Location | Description |
---|---|---|
BinaryInterface | core/lib/Drupal/Core/TypedData/Type/BinaryInterface.php | Interface for binary data. |
ComplexDataDefinitionInterface | core/lib/Drupal/Core/TypedData/ComplexDataDefinitionInterface.php | Interface for complex data definitions. |
ComplexDataInterface | core/lib/Drupal/Core/TypedData/ComplexDataInterface.php | Interface for complex data; i.e. data containing named and typed properties. |
DataDefinitionInterface | core/lib/Drupal/Core/TypedData/DataDefinitionInterface.php | Interface for data definitions. |
DataReferenceDefinitionInterface | core/lib/Drupal/Core/TypedData/DataReferenceDefinitionInterface.php | Interface for typed data references. |
DateTimeInterface | core/lib/Drupal/Core/TypedData/Type/DateTimeInterface.php | Interface for dates, optionally including a time. |
DurationInterface | core/lib/Drupal/Core/TypedData/Type/DurationInterface.php | Interface for durations. |
FloatInterface | core/lib/Drupal/Core/TypedData/Type/FloatInterface.php | Interface for floating-point numbers. |
IntegerInterface | core/lib/Drupal/Core/TypedData/Type/IntegerInterface.php | Interface for integer numbers. |
ListDataDefinitionInterface | core/lib/Drupal/Core/TypedData/ListDataDefinitionInterface.php | Interface for data definitions of lists. |
ListInterface | core/lib/Drupal/Core/TypedData/ListInterface.php | Interface for a list of typed data. |
PrimitiveInterface | core/lib/Drupal/Core/TypedData/PrimitiveInterface.php | Interface for primitive data. |
StringInterface | core/lib/Drupal/Core/TypedData/Type/StringInterface.php | Interface for strings. |
TypedDataInterface | core/lib/Drupal/Core/TypedData/TypedDataInterface.php | Interface for typed data objects. |
UriInterface | core/lib/Drupal/Core/TypedData/Type/UriInterface.php | Interface for URIs. |
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!core.api.php/group/typed_data/8.1.x