Updated to Drupal 8.5. Core Media not yet in use.
[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\DataDefinition;
7
8 /**
9  * A typed data definition class for defining field items.
10  *
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.
15  */
16 class FieldItemDataDefinition extends DataDefinition implements FieldItemDataDefinitionInterface {
17
18   /**
19    * The field definition the item definition belongs to.
20    *
21    * @var \Drupal\Core\Field\FieldDefinitionInterface
22    */
23   protected $fieldDefinition;
24
25   /**
26    * {@inheritdoc}
27    */
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".');
33     }
34
35     $field_definition = BaseFieldDefinition::create($parts[1]);
36     return $field_definition->getItemDefinition();
37   }
38
39   /**
40    * Creates a new field item definition.
41    *
42    * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
43    *   The field definition the item definition belongs to.
44    *
45    * @return static
46    */
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;
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function getPropertyDefinition($name) {
58     return $this->fieldDefinition->getFieldStorageDefinition()->getPropertyDefinition($name);
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function getPropertyDefinitions() {
65     return $this->fieldDefinition->getFieldStorageDefinition()->getPropertyDefinitions();
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function getMainPropertyName() {
72     return $this->fieldDefinition->getFieldStorageDefinition()->getMainPropertyName();
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function getFieldDefinition() {
79     return $this->fieldDefinition;
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function setFieldDefinition($field_definition) {
86     $this->fieldDefinition = $field_definition;
87     return $this;
88   }
89
90 }