diff --git a/core/lib/Drupal/Core/CoreBundle.php b/core/lib/Drupal/Core/CoreBundle.php index ef12b11..092ebae 100644 --- a/core/lib/Drupal/Core/CoreBundle.php +++ b/core/lib/Drupal/Core/CoreBundle.php @@ -223,7 +223,8 @@ public function build(ContainerBuilder $container) { ->addArgument(array()) ->addArgument(array()); - $container->register('serializer.normalizer.entity', 'Drupal\Core\Serialization\EntityNormalizer')->addTag('normalizer'); + $container->register('serializer.normalizer.complex_data', 'Drupal\Core\Serialization\ComplexDataNormalizer')->addTag('normalizer'); + $container->register('serializer.normalizer.typed_data', 'Drupal\Core\Serialization\TypedDataNormalizer')->addTag('normalizer'); $container->register('serializer.encoder.json', 'Drupal\Core\Serialization\JsonEncoder')->addTag('encoder'); $container->register('flood', 'Drupal\Core\Flood\DatabaseBackend') diff --git a/core/lib/Drupal/Core/Serialization/EntityNormalizer.php b/core/lib/Drupal/Core/Serialization/ComplexDataNormalizer.php similarity index 61% rename from core/lib/Drupal/Core/Serialization/EntityNormalizer.php rename to core/lib/Drupal/Core/Serialization/ComplexDataNormalizer.php index c007c3f..d358935 100644 --- a/core/lib/Drupal/Core/Serialization/EntityNormalizer.php +++ b/core/lib/Drupal/Core/Serialization/ComplexDataNormalizer.php @@ -2,14 +2,13 @@ /** * @file - * Contains Drupal\Core\Serialization\EntityNormalizer. + * Contains Drupal\Core\Serialization\ComplexDataNormalizer. */ namespace Drupal\Core\Serialization; -use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Serialization\NormalizerBase; use Symfony\Component\Serializer\Exception\RuntimeException; -use Symfony\Component\Serializer\Normalizer\NormalizerInterface; /** * Converts the Drupal entity object structures to a normalized array. @@ -21,20 +20,23 @@ * that module can register a new Normalizer and give it a higher priority than * this one. */ -class EntityNormalizer implements NormalizerInterface { +class ComplexDataNormalizer extends NormalizerBase { /** - * Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize(). + * The interface or class that this Normalizer supports. + * + * @var string */ - public function normalize($object, $format = NULL) { - return $object->getPropertyValues(); - } + protected static $supportedInterfaceOrClass = 'Drupal\Core\TypedData\ComplexDataInterface'; /** - * Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::supportsNormalization(). + * Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize(). */ - public function supportsNormalization($data, $format = NULL) { - return is_object($data) && ($data instanceof EntityInterface); + public function normalize($object, $format = NULL) { + foreach ($object as $name => $field) { + $attributes[$name] = $this->serializer->normalize($field, $format); + } + return $attributes; } } diff --git a/core/lib/Drupal/Core/Serialization/NormalizerBase.php b/core/lib/Drupal/Core/Serialization/NormalizerBase.php new file mode 100644 index 0000000..f256b0e --- /dev/null +++ b/core/lib/Drupal/Core/Serialization/NormalizerBase.php @@ -0,0 +1,34 @@ +getValue(); + } + +} diff --git a/core/modules/system/lib/Drupal/system/Tests/Serialization/EntitySerializationTest.php b/core/modules/system/lib/Drupal/system/Tests/Serialization/EntitySerializationTest.php index 305fe3a..cc1b76d 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Serialization/EntitySerializationTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Serialization/EntitySerializationTest.php @@ -8,7 +8,6 @@ namespace Drupal\system\Tests\Serialization; use Drupal\simpletest\WebTestBase; -use Drupal\Core\Serialization\EntityNormalizer; class EntitySerializationTest extends WebTestBase { @@ -59,7 +58,7 @@ protected function setUp() { * Test the normalize function. */ public function testNormalize() { - $normalizer = new EntityNormalizer(); + $serializer = drupal_container()->get('serializer'); $expected = array( 'id' => array( @@ -89,8 +88,11 @@ public function testNormalize() { ), ); - $normalized = $normalizer->normalize($this->entity); - $this->assertEqual($expected, $normalized, 'EntityNormalizer produces expected array.'); + $normalized = $serializer->normalize($this->entity); + foreach (array_keys($expected) as $fieldName) { + $this->assertEqual($expected[$fieldName], $normalized[$fieldName], "ComplexDataNormalizer produces expected array for $fieldName."); + } + $this->assertEqual(array_keys($expected), array_keys($normalized), 'No unexpected data is added to the normalized array.'); } /** @@ -98,12 +100,11 @@ public function testNormalize() { */ public function testSerialize() { $serializer = drupal_container()->get('serializer'); - $normalizer = new EntityNormalizer(); - // Test that Serializer responds using the EntityNormalizer and - // JsonEncoder. The output of EntityNormalizer::normalize() is tested + // Test that Serializer responds using the ComplexDataNormalizer and + // JsonEncoder. The output of ComplexDataNormalizer::normalize() is tested // elsewhere, so we can just assume that it works properly here. - $normalized = $normalizer->normalize($this->entity, 'json'); + $normalized = $serializer->normalize($this->entity, 'json'); $expected = json_encode($normalized); // Test 'json'. $actualJson = $serializer->serialize($this->entity, 'json');