Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldType / MapItem.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldType;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\Core\Field\FieldItemBase;
7
8 /**
9  * Defines the 'map' entity field type.
10  *
11  * @FieldType(
12  *   id = "map",
13  *   label = @Translation("Map"),
14  *   description = @Translation("An entity field for storing a serialized array of values."),
15  *   no_ui = TRUE
16  * )
17  */
18 class MapItem extends FieldItemBase {
19
20   /**
21    * {@inheritdoc}
22    */
23   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
24     // The properties are dynamic and can not be defined statically.
25     return [];
26   }
27
28   /**
29    * {@inheritdoc}
30    */
31   public static function schema(FieldStorageDefinitionInterface $field_definition) {
32     return [
33       'columns' => [
34         'value' => [
35           'type' => 'blob',
36           'size' => 'big',
37           'serialize' => TRUE,
38         ],
39       ],
40     ];
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function toArray() {
47     // The default implementation of toArray() only returns known properties.
48     // For a map, return everything as the properties are not pre-defined.
49     return $this->getValue();
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function setValue($values, $notify = TRUE) {
56     $this->values = [];
57     if (!isset($values)) {
58       return;
59     }
60
61     if (!is_array($values)) {
62       if ($values instanceof MapItem) {
63         $values = $values->getValue();
64       }
65       else {
66         $values = unserialize($values);
67       }
68     }
69
70     $this->values = $values;
71
72     // Notify the parent of any changes.
73     if ($notify && isset($this->parent)) {
74       $this->parent->onChange($this->name);
75     }
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function __get($name) {
82     if (!isset($this->values[$name])) {
83       $this->values[$name] = [];
84     }
85
86     return $this->values[$name];
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function __set($name, $value) {
93     if (isset($value)) {
94       $this->values[$name] = $value;
95     }
96     else {
97       unset($this->values[$name]);
98     }
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public static function mainPropertyName() {
105     // A map item has no main property.
106     return NULL;
107   }
108
109   /**
110    * {@inheritdoc}
111    */
112   public function isEmpty() {
113     return empty($this->values);
114   }
115
116 }