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."),
16 * list_class = "\Drupal\Core\Field\MapFieldItemList",
19 class MapItem extends FieldItemBase {
24 public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
25 // The properties are dynamic and can not be defined statically.
32 public static function schema(FieldStorageDefinitionInterface $field_definition) {
47 public function toArray() {
48 // The default implementation of toArray() only returns known properties.
49 // For a map, return everything as the properties are not pre-defined.
50 return $this->getValue();
56 public function setValue($values, $notify = TRUE) {
58 if (!isset($values)) {
62 if (!is_array($values)) {
63 if ($values instanceof MapItem) {
64 $values = $values->getValue();
67 $values = unserialize($values);
71 $this->values = $values;
73 // Notify the parent of any changes.
74 if ($notify && isset($this->parent)) {
75 $this->parent->onChange($this->name);
82 public function __get($name) {
83 if (!isset($this->values[$name])) {
84 $this->values[$name] = [];
87 return $this->values[$name];
93 public function __set($name, $value) {
95 $this->values[$name] = $value;
98 unset($this->values[$name]);
105 public static function mainPropertyName() {
106 // A map item has no main property.
113 public function isEmpty() {
114 return empty($this->values);