3 namespace Drupal\Core\Field\Plugin\Field\FieldType;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\Core\Field\FieldItemBase;
9 * Defines the 'map' entity field type.
13 * label = @Translation("Map"),
14 * description = @Translation("An entity field for storing a serialized array of values."),
18 class MapItem extends FieldItemBase {
23 public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
24 // The properties are dynamic and can not be defined statically.
31 public static function schema(FieldStorageDefinitionInterface $field_definition) {
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();
55 public function setValue($values, $notify = TRUE) {
57 if (!isset($values)) {
61 if (!is_array($values)) {
62 if ($values instanceof MapItem) {
63 $values = $values->getValue();
66 $values = unserialize($values);
70 $this->values = $values;
72 // Notify the parent of any changes.
73 if ($notify && isset($this->parent)) {
74 $this->parent->onChange($this->name);
81 public function __get($name) {
82 if (!isset($this->values[$name])) {
83 $this->values[$name] = [];
86 return $this->values[$name];
92 public function __set($name, $value) {
94 $this->values[$name] = $value;
97 unset($this->values[$name]);
104 public static function mainPropertyName() {
105 // A map item has no main property.
112 public function isEmpty() {
113 return empty($this->values);