diff --git a/core/lib/Drupal/Core/TypedData/Plugin/DataType/Map.php b/core/lib/Drupal/Core/TypedData/Plugin/DataType/Map.php index 5d1de528e7..a95c14b30e 100644 --- a/core/lib/Drupal/Core/TypedData/Plugin/DataType/Map.php +++ b/core/lib/Drupal/Core/TypedData/Plugin/DataType/Map.php @@ -2,8 +2,6 @@ namespace Drupal\Core\TypedData\Plugin\DataType; -use Drupal\Core\TypedData\DataDefinition; -use Drupal\Core\TypedData\MapDataDefinition; use Drupal\Core\TypedData\TypedData; use Drupal\Core\TypedData\ComplexDataInterface; @@ -162,33 +160,6 @@ public function getProperties($include_computed = FALSE) { $properties[$name] = $this->get($name); } } - - // In case someone uses the Map data type or the MapItem field type, - // $this->definition->getPropertyDefinitions() is empty, as these maps meant to have different - // values per entity. - // Instead iterate over the values to define properties on the fly. By doing so normalization - // will work automatically. - if (empty($properties) && $this->getDataDefinition() instanceof MapDataDefinition) { - foreach ($this->values as $key => $value) { - if (!empty($value) && is_array($value)) { - // Recursively create the data definition for each element of the child array. - $properties[$key] = $this->getTypedDataManager()->create( - MapDataDefinition::create(), - $value, - $key - ); - } - else { - $properties[$key] = $this->getTypedDataManager()->create( - // For scalar values we use 'any' as we don't have more information about it. - // If you have more information about your properties, please use setPropertyDefinition. - DataDefinition::create('any'), - $value, - $key - ); - } - } - } return $properties; } diff --git a/core/modules/serialization/serialization.services.yml b/core/modules/serialization/serialization.services.yml index dca6094787..1e28a28b2c 100644 --- a/core/modules/serialization/serialization.services.yml +++ b/core/modules/serialization/serialization.services.yml @@ -71,6 +71,10 @@ services: class: Drupal\serialization\Normalizer\TypedDataNormalizer tags: - { name: normalizer } + serializer.normalizer.map: + class: Drupal\serialization\Normalizer\MapNormalizer + tags: + - { name: normalizer, priority: 25} serializer.encoder.json: class: Drupal\serialization\Encoder\JsonEncoder tags: diff --git a/core/modules/serialization/src/Normalizer/MapNormalizer.php b/core/modules/serialization/src/Normalizer/MapNormalizer.php new file mode 100644 index 0000000000..115bb64ffb --- /dev/null +++ b/core/modules/serialization/src/Normalizer/MapNormalizer.php @@ -0,0 +1,34 @@ +getDataDefinition(); + if ($definition instanceof ComplexDataDefinitionInterface && empty($definition->g())) { + return TRUE; + } + } + return FALSE; + } + +}