Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Field / TypedData / FieldItemDataDefinition.php
1 <?php
2
3 namespace Drupal\Core\Field\TypedData;
4
5 use Drupal\Core\Field\BaseFieldDefinition;
6 use Drupal\Core\TypedData\ComplexDataDefinitionInterface;
7 use Drupal\Core\TypedData\DataDefinition;
8
9 /**
10  * A typed data definition class for defining field items.
11  *
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.
16  */
17 class FieldItemDataDefinition extends DataDefinition implements ComplexDataDefinitionInterface {
18
19   /**
20    * The field definition the item definition belongs to.
21    *
22    * @var \Drupal\Core\Field\FieldDefinitionInterface
23    */
24   protected $fieldDefinition;
25
26   /**
27    * {@inheritdoc}
28    */
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".');
34     }
35
36     $field_definition = BaseFieldDefinition::create($parts[1]);
37     return $field_definition->getItemDefinition();
38   }
39
40   /**
41    * Creates a new field item definition.
42    *
43    * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
44    *   The field definition the item definition belongs to.
45    *
46    * @return static
47    */
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;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function getPropertyDefinition($name) {
59     return $this->fieldDefinition->getFieldStorageDefinition()->getPropertyDefinition($name);
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function getPropertyDefinitions() {
66     return $this->fieldDefinition->getFieldStorageDefinition()->getPropertyDefinitions();
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function getMainPropertyName() {
73     return $this->fieldDefinition->getFieldStorageDefinition()->getMainPropertyName();
74   }
75
76   /**
77    * Gets the field item's field definition.
78    *
79    * @return \Drupal\Core\Field\FieldDefinitionInterface
80    *   The field definition for this field item.
81    */
82   public function getFieldDefinition() {
83     return $this->fieldDefinition;
84   }
85
86 }