3 namespace Drupal\Core\Field\TypedData;
5 use Drupal\Core\Field\BaseFieldDefinition;
6 use Drupal\Core\TypedData\DataDefinition;
9 * A typed data definition class for defining field items.
11 * This class is just a small wrapper around field definitions to expose
12 * metadata about field item's via the Typed Data API. As the work is done
13 * by the field definitions, this class does not benefit and thus does not
14 * extend from MapDefinition or ComplexDataDefinitionBase.
16 class FieldItemDataDefinition extends DataDefinition implements FieldItemDataDefinitionInterface {
19 * The field definition the item definition belongs to.
21 * @var \Drupal\Core\Field\FieldDefinitionInterface
23 protected $fieldDefinition;
28 public static function createFromDataType($data_type) {
29 // The data type of a field item is in the form of "field_item:$field_type".
30 $parts = explode(':', $data_type, 2);
31 if ($parts[0] != 'field_item') {
32 throw new \InvalidArgumentException('Data type must be in the form of "field_item:FIELD_TYPE".');
35 $field_definition = BaseFieldDefinition::create($parts[1]);
36 return $field_definition->getItemDefinition();
40 * Creates a new field item definition.
42 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
43 * The field definition the item definition belongs to.
47 public static function create($field_definition) {
48 $definition['type'] = 'field_item:' . $field_definition->getType();
49 $item_definition = new static($definition);
50 $item_definition->fieldDefinition = $field_definition;
51 return $item_definition;
57 public function getPropertyDefinition($name) {
58 return $this->fieldDefinition->getFieldStorageDefinition()->getPropertyDefinition($name);
64 public function getPropertyDefinitions() {
65 return $this->fieldDefinition->getFieldStorageDefinition()->getPropertyDefinitions();
71 public function getMainPropertyName() {
72 return $this->fieldDefinition->getFieldStorageDefinition()->getMainPropertyName();
78 public function getFieldDefinition() {
79 return $this->fieldDefinition;
85 public function setFieldDefinition($field_definition) {
86 $this->fieldDefinition = $field_definition;