diff --git a/core/lib/Drupal/Core/Serialization/EntityReferenceHandlerAwareInterface.php b/core/lib/Drupal/Core/Serialization/EntityReferenceHandlerAwareInterface.php deleted file mode 100644 index 355cbc9..0000000 --- a/core/lib/Drupal/Core/Serialization/EntityReferenceHandlerAwareInterface.php +++ /dev/null @@ -1,21 +0,0 @@ -setEntityReferenceHandlerPluginManager($erh_plugin_manager); - } - } - parent::__construct($normalizers, $encoders); - } -} diff --git a/core/modules/jsonld/jsonld.info b/core/modules/jsonld/jsonld.info index fce4688..6e89779 100644 --- a/core/modules/jsonld/jsonld.info +++ b/core/modules/jsonld/jsonld.info @@ -4,3 +4,4 @@ package = Core version = VERSION core = 8.x dependencies[] = rdf +dependencies[] = serialization diff --git a/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityNormalizer.php b/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityNormalizer.php index acc75c3..11d021b 100644 --- a/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityNormalizer.php +++ b/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityNormalizer.php @@ -7,7 +7,6 @@ namespace Drupal\jsonld; -use Drupal\Core\Serialization\EntityReferenceHandlerAwareInterface; use Drupal\jsonld\JsonldNormalizerBase; use Drupal\serialization\Plugin\Type\EntityReferenceHandlerPluginManager; use Drupal\rdf\RdfMappingException; @@ -17,7 +16,7 @@ /** * Converts the Drupal entity object structure to JSON-LD array structure. */ -class JsonldEntityNormalizer extends JsonldNormalizerBase implements DenormalizerInterface, EntityReferenceHandlerAwareInterface { +class JsonldEntityNormalizer extends JsonldNormalizerBase implements DenormalizerInterface { /** * The interface or class that this Normalizer supports. @@ -27,20 +26,6 @@ class JsonldEntityNormalizer extends JsonldNormalizerBase implements Denormalize protected static $supportedInterfaceOrClass = 'Drupal\Core\Entity\EntityInterface'; /** - * The plugin manager for entity reference handlers. - * - * @var EntityReferenceHandlerPluginManager - */ - protected $entityReferenceHandlerPluginManager; - - /** - * Implements \Drupal\Core\Serialization\EntityReferenceHandlerAwareInterface::setEntityReferenceHandlerPluginManager() - */ - public function setEntityReferenceHandlerPluginManager(EntityReferenceHandlerPluginManager $erh_plugin_manager) { - $this->entityReferenceHandlerPluginManager = $erh_plugin_manager; - } - - /** * Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize() */ public function normalize($entity, $format = NULL, array $context = array()) { @@ -104,6 +89,7 @@ public function denormalize($data, $class, $format = NULL, array $context = arra } } + $entityreference_settings = serialization_get_entityreference_deserialize_settings($entity->entityType(), $entity->bundle(), $format); // For each attribute in the JSON-LD, add the values as fields to the newly // created entity. It is assumed that the JSON attribute names are the same // as the site's field names. @@ -128,10 +114,8 @@ public function denormalize($data, $class, $format = NULL, array $context = arra // The vnd.drupal.ld+json mime type will always use language keys, per // http://drupal.org/node/1838700. foreach ($incomingFieldValues as $langcode => $incomingFieldItems) { - // @todo Make it possible to switch between different entityreference - // handlers based on the field which is being denormalized. $context = array( - 'entityreference_handler' => $this->entityReferenceHandlerPluginManager->createInstance('configured_value'), + 'entityreference_handler' => $entityreference_settings->getHandler($fieldName), ); $fieldValue = $this->serializer->denormalize($incomingFieldItems, $fieldItemClass, $format, $context); $entity->getTranslation($langcode) diff --git a/core/modules/jsonld/lib/Drupal/jsonld/Tests/NormalizeDenormalizeTest.php b/core/modules/jsonld/lib/Drupal/jsonld/Tests/NormalizeDenormalizeTest.php index a9e057d..db76ea2 100644 --- a/core/modules/jsonld/lib/Drupal/jsonld/Tests/NormalizeDenormalizeTest.php +++ b/core/modules/jsonld/lib/Drupal/jsonld/Tests/NormalizeDenormalizeTest.php @@ -145,6 +145,13 @@ function testDenormalize() { $bundle_uri = $schema->bundle('entity_test', 'entity_test')->getUri(); $incoming_data = array( '@type' => $bundle_uri, + 'user_id' => array( + 'de' => array( + array( + '@id' => 'http://example.com/user/foo', + ), + ), + ), 'name' => array( 'en' => array( array( @@ -167,12 +174,26 @@ function testDenormalize() { ), ); + // Set the entity reference handlers for entity reference fields. + $values = array( + 'targetEntityType' => 'entity_test', + 'bundle' => 'entity_test', + 'format' => static::$format, + ); + $config = entity_create('entityreference_deserialize_settings', $values); + $config->setHandler('user_id', 'configured_value'); + $config->save(); + // Test valid request. $entity = $this->normalizers['entity']->denormalize($incoming_data, 'Drupal\Core\Entity\EntityNG', static::$format); $this->assertEqual('entity_test', $entity->bundle(), "Denormalize creates entity with correct bundle."); $this->assertEqual($incoming_data['name']['en'], $entity->get('name')->getValue(), "Translatable field denormalized correctly in default language."); $this->assertEqual($incoming_data['name']['de'], $entity->getTranslation('de')->get('name')->getValue(), "Translatable field denormalized correctly in translation language."); $this->assertEqual($incoming_data['field_test_text']['und'], $entity->get('field_test_text')->getValue(), "Untranslatable field denormalized correctly."); + // For now, we just test that the user_id is equal to one. + // @todo Fix this once configured_value entity reference handler is + // enhanced. + $this->assertEqual(array(array('value' => 1)), $entity->getTranslation('de')->get('user_id')->getValue(), "Entity reference field denormalized correctly."); // Test request without @type. unset($incoming_data['@type']); diff --git a/core/modules/serialization/lib/Drupal/serialization/Plugin/Core/Entity/EntityReferenceDeserializeSettings.php b/core/modules/serialization/lib/Drupal/serialization/Plugin/Core/Entity/EntityReferenceDeserializeSettings.php new file mode 100644 index 0000000..edbbf6b --- /dev/null +++ b/core/modules/serialization/lib/Drupal/serialization/Plugin/Core/Entity/EntityReferenceDeserializeSettings.php @@ -0,0 +1,119 @@ +targetEntityType . '.' . $this->bundle . '.' . $this->format; + } + + /** + * Overrides \Drupal\config\ConfigEntityBase::save(). + */ + public function save() { + // Build an ID if none is set. + if (empty($this->id)) { + $this->id = $this->id(); + } + return parent::save(); + } + + /** + * Sets the Entity Reference Handler to use when deserializing this field. + * + * @param $field_name + * Name of the field which the handler will handle. + * @param $handler_name + * The plugin id of the handler. + */ + public function setHandler($field_name, $handler_name) { + $this->entityreferenceHandlers[$field_name] = $handler_name; + } + + /** + * Gets the Entity Reference Handler to use when deserializing this field. + * + * @param $field_name + * + * @return mixed + */ + public function getHandler($field_name) { + $plugin_manager = drupal_container()->get('plugin.manager.serialization.entityreference_handler'); + if (isset($this->entityreferenceHandlers[$field_name])) { + $handler_name = $this->entityreferenceHandlers[$field_name]; + return $plugin_manager->createInstance($handler_name); + } + return FALSE; + } + +} diff --git a/core/modules/serialization/lib/Drupal/serialization/SerializationBundle.php b/core/modules/serialization/lib/Drupal/serialization/SerializationBundle.php index 6f872b3..d92a550 100644 --- a/core/modules/serialization/lib/Drupal/serialization/SerializationBundle.php +++ b/core/modules/serialization/lib/Drupal/serialization/SerializationBundle.php @@ -9,7 +9,6 @@ use Drupal\Core\DependencyInjection\Compiler\RegisterSerializationClassesPass; use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\HttpKernel\Bundle\Bundle; /** @@ -24,10 +23,9 @@ public function build(ContainerBuilder $container) { // Register the entity reference handler plugin manager class. $container->register('plugin.manager.serialization.entityreference_handler', 'Drupal\serialization\Plugin\Type\EntityReferenceHandlerPluginManager'); // Add Serializer with arguments to be replaced in the compiler pass. - $container->register('serializer', 'Drupal\Core\Serialization\Serializer') + $container->register('serializer', 'Symfony\Component\Serializer\Serializer') ->addArgument(array()) - ->addArgument(array()) - ->addArgument(new Reference('plugin.manager.serialization.entityreference_handler')); + ->addArgument(array()); $container->register('serializer.normalizer.complex_data', 'Drupal\Core\Serialization\ComplexDataNormalizer')->addTag('normalizer'); $container->register('serializer.normalizer.list', 'Drupal\Core\Serialization\ListNormalizer')->addTag('normalizer'); diff --git a/core/modules/serialization/lib/Drupal/serialization/Tests/EntityReferenceDeserializeSettingsTest.php b/core/modules/serialization/lib/Drupal/serialization/Tests/EntityReferenceDeserializeSettingsTest.php new file mode 100644 index 0000000..b6dcc12 --- /dev/null +++ b/core/modules/serialization/lib/Drupal/serialization/Tests/EntityReferenceDeserializeSettingsTest.php @@ -0,0 +1,47 @@ + 'Entity Reference Deserialize Settings Test', + 'description' => "Tests the Entity Reference Deserialize Settings config object.", + 'group' => 'Serialization', + ); + } + + public function testSetGet() { + $values = array( + 'targetEntityType' => 'entity_test', + 'bundle' => 'entity_test', + 'format' => 'drupal_jsonld', + ); + $id = implode('.', $values); + $config = entity_create('entityreference_deserialize_settings', $values); + $config->setHandler('user_id', 'configured_value'); + $config->save(); + + $loadedConfig = entity_load('entityreference_deserialize_settings', $id); + // Test that setHandler works. + $expected= array ( + 'user_id' => 'configured_value', + ); + $this->assertEqual($expected, $loadedConfig->get('entityreferenceHandlers'), 'setHandler() sets the appropriate handler.'); + + // Test that getHandler works. + $handler = $loadedConfig->getHandler('user_id'); + $this->assertTrue(($handler instanceof ConfiguredValue), 'getHandler() returns an instance of the specified handler.'); + } +} diff --git a/core/modules/serialization/serialization.module b/core/modules/serialization/serialization.module index b3d9bbc..4d90be8 100644 --- a/core/modules/serialization/serialization.module +++ b/core/modules/serialization/serialization.module @@ -1 +1,38 @@ $entity_type, + 'bundle' => $bundle, + 'format' => $format, + )); + } + + return $settings; +} \ No newline at end of file