3 namespace Drupal\Core\Field\TypedData;
5 use Drupal\Core\Field\BaseFieldDefinition;
6 use Drupal\Core\TypedData\ComplexDataDefinitionInterface;
7 use Drupal\Core\TypedData\DataDefinition;
10 * A typed data definition class for defining field items.
12 * This class is just a small wrapper around field definitions to expose
13 * metadata about field item's via the Typed Data API. As the work is done
14 * by the field definitions, this class does not benefit and thus does not
15 * extend from MapDefinition or ComplexDataDefinitionBase.
17 class FieldItemDataDefinition extends DataDefinition implements ComplexDataDefinitionInterface {
20 * The field definition the item definition belongs to.
22 * @var \Drupal\Core\Field\FieldDefinitionInterface
24 protected $fieldDefinition;
29 public static function createFromDataType($data_type) {
30 // The data type of a field item is in the form of "field_item:$field_type".
31 $parts = explode(':', $data_type, 2);
32 if ($parts[0] != 'field_item') {
33 throw new \InvalidArgumentException('Data type must be in the form of "field_item:FIELD_TYPE".');
36 $field_definition = BaseFieldDefinition::create($parts[1]);
37 return $field_definition->getItemDefinition();
41 * Creates a new field item definition.
43 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
44 * The field definition the item definition belongs to.
48 public static function create($field_definition) {
49 $definition['type'] = 'field_item:' . $field_definition->getType();
50 $item_definition = new static($definition);
51 $item_definition->fieldDefinition = $field_definition;
52 return $item_definition;
58 public function getPropertyDefinition($name) {
59 return $this->fieldDefinition->getFieldStorageDefinition()->getPropertyDefinition($name);
65 public function getPropertyDefinitions() {
66 return $this->fieldDefinition->getFieldStorageDefinition()->getPropertyDefinitions();
72 public function getMainPropertyName() {
73 return $this->fieldDefinition->getFieldStorageDefinition()->getMainPropertyName();
77 * Gets the field item's field definition.
79 * @return \Drupal\Core\Field\FieldDefinitionInterface
80 * The field definition for this field item.
82 public function getFieldDefinition() {
83 return $this->fieldDefinition;