diff --git a/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityNormalizer.php b/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityNormalizer.php index 670b35a..11004af 100644 --- a/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityNormalizer.php +++ b/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityNormalizer.php @@ -89,7 +89,6 @@ public function denormalize($data, $class, $format = null, array $context = arra } } - $entityreference_settings = serialization_get_entityreference_handler_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. @@ -114,12 +113,14 @@ 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) { - $context = array(); $field_definition = $entity->getPropertyDefinition($fieldName); if ($field_definition['type'] == 'entity_reference_field') { - $handler_settings = $entityreference_settings->getHandler($fieldName); - $handler_settings['settings']['target_entity_type'] = $field_definition['settings']['target_type']; - $context['entityreference_handler'] = $this->entityReferenceHandlerPluginManager->createInstance($handler_settings['plugin_id'], $handler_settings['settings']); + $options = array( + 'field' => $field_definition, + 'field_name' => $fieldName, + 'format' => $format, + ); + $context['entityreference_handler'] = $this->entityReferenceHandlerPluginManager->getInstance($options); } $fieldValue = $this->serializer->denormalize($incomingFieldItems, $fieldItemClass, $format, $context); $entity->getTranslation($langcode) diff --git a/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityReferenceNormalizer.php b/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityReferenceNormalizer.php index 29b2445..e91879f 100644 --- a/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityReferenceNormalizer.php +++ b/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityReferenceNormalizer.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\jsonld\JsonldEntityReferenceNormalizer. + * Contains \Drupal\jsonld\JsonldEntityReferenceNormalizer. */ namespace Drupal\jsonld; @@ -14,11 +14,19 @@ use ReflectionClass; use Symfony\Component\Serializer\Exception\RuntimeException; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; +use Drupal\serialization\Plugin\serialization\entityreference_handler\UuidReferenceInterface; /** * Converts an EntityReferenceItem to a JSON-LD array structure. */ -class JsonldEntityReferenceNormalizer extends JsonldNormalizerBase implements DenormalizerInterface { +class JsonldEntityReferenceNormalizer extends JsonldNormalizerBase implements DenormalizerInterface, UuidReferenceInterface { + + /** + * The incoming data. + * + * @var array + */ + protected $data; /** * The interface or class that this Normalizer supports. @@ -30,7 +38,7 @@ class JsonldEntityReferenceNormalizer extends JsonldNormalizerBase implements De /** * Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize() */ - public function normalize($object, $format = null, array $context = array()) { + public function normalize($object, $format = NULL, array $context = array()) { $return = array(); // @todo Since the context parameter was added to Serilizer's interface, as // requested in https://github.com/symfony/symfony/pull/4938, then instead @@ -52,12 +60,13 @@ public function normalize($object, $format = null, array $context = array()) { /** * Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize() */ - public function denormalize($data, $class, $format = null, array $context = array()) { + public function denormalize($data, $class, $format = NULL, array $context = array()) { $return = array(); $entityreference_handler = $context['entityreference_handler']; foreach ($data as $node) { - $return[]['target_id'] = $entityreference_handler->getEntityId($node, $format, $this->serializer); + $this->data = $node; + $return[]['target_id'] = $entityreference_handler->getEntityId($this); } return $return; } @@ -70,4 +79,17 @@ public function supportsDenormalization($data, $type, $format = NULL) { return in_array($format, static::$format) && ($reflection->getName() == static::$supportedInterfaceOrClass || $reflection->isSubclassOf(static::$supportedInterfaceOrClass)); } + /** + * Implements \Drupal\serialization\ReferenceHandler\UuidExtractorInterface::getUuid(). + */ + public function getUuid() { + if (isset($this->data['uuid'])) { + $uuid = $this->data['uuid']; + while (is_array($uuid)) { + $uuid = reset($uuid); + } + return $uuid; + } + } + } diff --git a/core/modules/jsonld/lib/Drupal/jsonld/Tests/DenormalizeTest.php b/core/modules/jsonld/lib/Drupal/jsonld/Tests/DenormalizeTest.php index 92ea50e..08130d4 100644 --- a/core/modules/jsonld/lib/Drupal/jsonld/Tests/DenormalizeTest.php +++ b/core/modules/jsonld/lib/Drupal/jsonld/Tests/DenormalizeTest.php @@ -98,9 +98,8 @@ function testBasicDenormalize() { */ function testEntityReferenceDenormalize() { // Configure the entity reference handlers. - $handler_settings = serialization_get_entityreference_handler_settings($this->entityType, $this->bundle, $this->format); - $uid = 1; - $handler_settings->setHandler('user_id', 'configured_value', array('value' => $uid)); + $handler_settings = entity_create('entityreference_handler_settings', array('format' => $this->format)); + $handler_settings->setHandler('user_id', 'null_value'); $handler_settings->save(); $incoming_data = array( @@ -125,8 +124,8 @@ function testEntityReferenceDenormalize() { $serializer = $this->container->get('serializer'); $entity = $serializer->denormalize($incoming_data, 'Drupal\Core\Entity\EntityNG', $this->format); - // Test the configured value handler. - $this->assertEqual(array(array('target_id' => $uid)), $entity->get('user_id')->getValue(), 'The configured value entity reference handler sets field to correct value.'); + // Test the null value handler. + $this->assertEqual(array(NULL), $entity->get('user_id')->getValue(), 'The null value entity reference handler works.'); } /** @@ -154,10 +153,10 @@ function testUuidEntityReferenceHandler() { field_create_instance($instance); // Configure the entity reference handler. - $handler_settings = serialization_get_entityreference_handler_settings($this->entityType, $this->bundle, $this->format); + $handler_settings = entity_create('entityreference_handler_settings', array('format' => $this->format)); // The uuid_key is set to 'guid' to demonstrate how entity reference // handler configuration works. - $handler_settings->setHandler('field_test_entity_reference', 'uuid', array('uuid_key' => 'guid')); + $handler_settings->setHandler('field_test_entity_reference', 'uuid'); $handler_settings->save(); // Create an entity to get the UUID from. @@ -179,7 +178,13 @@ function testUuidEntityReferenceHandler() { 'und' => array( array( '@id' => 'http://example.org/bar', - 'guid' => $entity->uuid(), + 'uuid' => array( + 'und' => array( + array( + 'value' => $entity->uuid(), + ), + ), + ), ), ), ), diff --git a/core/modules/rest/lib/Drupal/rest/Tests/CreateTest.php b/core/modules/rest/lib/Drupal/rest/Tests/CreateTest.php index cb35d69..c5b298a 100644 --- a/core/modules/rest/lib/Drupal/rest/Tests/CreateTest.php +++ b/core/modules/rest/lib/Drupal/rest/Tests/CreateTest.php @@ -7,7 +7,6 @@ namespace Drupal\rest\Tests; -use Drupal\jsonld\Tests\JsonldTestSetupHelper; use Drupal\rest\Tests\RESTTestBase; /** @@ -34,16 +33,12 @@ public static function getInfo() { * Tests several valid and invalid create requests on all entity types. */ public function testCreate() { - // Get the mock Serializer. - $jsonld_setup_helper = new JsonldTestSetupHelper(); - $serializer = $jsonld_setup_helper->getSerializer(); - + $serializer = drupal_container()->get('serializer'); // @todo once EntityNG is implemented for other entity types test all other // entity types here as well. $entity_type = 'entity_test'; $this->enableService('entity:' . $entity_type); - $this->setupEntityReferenceHandlers(); // Create a user account that has the required permissions to create // resources via the web API. $account = $this->drupalCreateUser(array('restful post entity:' . $entity_type)); @@ -64,6 +59,9 @@ public function testCreate() { $loaded_entity = entity_load($entity_type, $id); $this->assertNotIdentical(FALSE, $loaded_entity, 'The new ' . $entity_type . ' was found in the database.'); $this->assertEqual($entity->uuid(), $loaded_entity->uuid(), 'UUID of created entity is correct.'); + // @todo Remove the user reference field for now until deserialization for + // entity references is implemented. + unset($entity_values['user_id']); foreach ($entity_values as $property => $value) { $actual_value = $loaded_entity->get($property)->value; $send_value = $entity->get($property)->value; diff --git a/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php b/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php index 0d763fb..f7dbdb6 100644 --- a/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php +++ b/core/modules/rest/lib/Drupal/rest/Tests/RESTTestBase.php @@ -151,21 +151,6 @@ protected function entityValues($entity_type) { } /** - * Saves configuration for entity reference handlers for deserialization. - */ - protected function setupEntityReferenceHandlers() { - // Set up the Entity Reference Handler for user_id field. - $values = array( - 'targetEntityType' => 'entity_test', - 'bundle' => 'entity_test', - 'format' => 'drupal_jsonld', - ); - $config = entity_create('entityreference_handler_settings', $values); - $config->setHandler('user_id', 'configured_value', array('value' => 1)); - $config->save(); - } - - /** * Enables the web service interface for a specific entity type. * * @param string|FALSE $resource_type diff --git a/core/modules/rest/lib/Drupal/rest/Tests/UpdateTest.php b/core/modules/rest/lib/Drupal/rest/Tests/UpdateTest.php index 129d8e2..fa65a2f 100644 --- a/core/modules/rest/lib/Drupal/rest/Tests/UpdateTest.php +++ b/core/modules/rest/lib/Drupal/rest/Tests/UpdateTest.php @@ -7,7 +7,6 @@ namespace Drupal\rest\Tests; -use Drupal\jsonld\Tests\JsonldTestSetupHelper; use Drupal\rest\Tests\RESTTestBase; /** @@ -34,15 +33,12 @@ public static function getInfo() { * Tests several valid and invalid partial update requests on test entities. */ public function testPatchUpdate() { - // Get the mock Serializer. - $jsonld_setup_helper = new JsonldTestSetupHelper(); - $serializer = $jsonld_setup_helper->getSerializer(); + $serializer = drupal_container()->get('serializer'); // @todo once EntityNG is implemented for other entity types test all other // entity types here as well. $entity_type = 'entity_test'; $this->enableService('entity:' . $entity_type); - $this->setupEntityReferenceHandlers(); // Create a user account that has the required permissions to create // resources via the web API. $account = $this->drupalCreateUser(array('restful patch entity:' . $entity_type)); @@ -102,15 +98,12 @@ public function testPatchUpdate() { * Tests several valid and invalid PUT update requests on test entities. */ public function testPutUpdate() { - // Get the mock Serializer. - $jsonld_setup_helper = new JsonldTestSetupHelper(); - $serializer = $jsonld_setup_helper->getSerializer(); + $serializer = drupal_container()->get('serializer'); // @todo once EntityNG is implemented for other entity types test all other // entity types here as well. $entity_type = 'entity_test'; $this->enableService('entity:' . $entity_type); - $this->setupEntityReferenceHandlers(); // Create a user account that has the required permissions to create // resources via the web API. $account = $this->drupalCreateUser(array('restful put entity:' . $entity_type)); @@ -134,6 +127,9 @@ public function testPutUpdate() { // Re-load the updated entity from the database. $entity = entity_load($entity_type, $entity->id(), TRUE); + // @todo Don't check the user reference field for now until deserialization + // for entity references is implemented. + unset($update_values['user_id']); foreach ($update_values as $property => $value) { $update_value = $update_entity->{$property}->value; $stored_value = $entity->{$property}->value; diff --git a/core/modules/serialization/lib/Drupal/serialization/Plugin/Core/Entity/EntityReferenceHandlerSettings.php b/core/modules/serialization/lib/Drupal/serialization/Plugin/Core/Entity/EntityReferenceHandlerSettings.php index 405bf12..5095a72 100644 --- a/core/modules/serialization/lib/Drupal/serialization/Plugin/Core/Entity/EntityReferenceHandlerSettings.php +++ b/core/modules/serialization/lib/Drupal/serialization/Plugin/Core/Entity/EntityReferenceHandlerSettings.php @@ -42,20 +42,6 @@ class EntityReferenceHandlerSettings extends ConfigEntityBase { public $uuid; /** - * Entity type to be deserialized. - * - * @var string - */ - public $targetEntityType; - - /** - * Bundle to be deserialized. - * - * @var string - */ - public $bundle; - - /** * The short name of the serialization format. * * @var string @@ -73,7 +59,7 @@ class EntityReferenceHandlerSettings extends ConfigEntityBase { * Overrides \Drupal\Core\Entity\Entity::id(). */ public function id() { - return $this->targetEntityType . '.' . $this->bundle . '.' . $this->format; + return $this->format; } /** @@ -94,14 +80,9 @@ public function save() { * Name of the field which the handler will handle. * @param $handler_name * The plugin id of the handler. - * @param $settings - * The settings to use when creating a handler plugin instance. */ - public function setHandler($field_name, $handler_name, array $settings = array()) { - $this->entityreferenceHandlers[$field_name] = array( - 'plugin_id' => $handler_name, - 'settings' => $settings, - ); + public function setHandler($field_name, $handler_name) { + $this->entityreferenceHandlers[$field_name] = $handler_name; } /** @@ -118,15 +99,8 @@ public function getHandler($field_name) { return $this->entityreferenceHandlers[$field_name]; } else { - // For now, return configured_value plugin whenever it isn't specified. // @todo Make it possible to configure a default per format. - return array( - 'plugin_id' => 'configured_value', - 'settings' => array( - 'value' => NULL, - ) - ); - //return FALSE; + return 'null_value'; } } diff --git a/core/modules/serialization/lib/Drupal/serialization/Plugin/EntityReferenceHandlerBase.php b/core/modules/serialization/lib/Drupal/serialization/Plugin/EntityReferenceHandlerBase.php deleted file mode 100644 index 5104269..0000000 --- a/core/modules/serialization/lib/Drupal/serialization/Plugin/EntityReferenceHandlerBase.php +++ /dev/null @@ -1,60 +0,0 @@ -defaultSettingsMerged && !array_key_exists($key, $this->configuration)) { - $this->mergeDefaults(); - } - return isset($this->configuration[$key]) ? $this->configuration[$key] : NULL; - } - - /** - * Merges default settings values into the configuration passed in. - */ - protected function mergeDefaults() { - $this->configuration += $this->getDefaultSettings(); - $this->defaultSettingsMerged = TRUE; - } - - /** - * Returns the default settings for the plugin. - * - * @return array - * The array of default setting values, keyed by setting names. - */ - public function getDefaultSettings() { - $definition = $this->getDefinition(); - return $definition['settings']; - } -} diff --git a/core/modules/serialization/lib/Drupal/serialization/Plugin/EntityReferenceHandlerInterface.php b/core/modules/serialization/lib/Drupal/serialization/Plugin/EntityReferenceHandlerInterface.php index fc2cf8c..c633703 100644 --- a/core/modules/serialization/lib/Drupal/serialization/Plugin/EntityReferenceHandlerInterface.php +++ b/core/modules/serialization/lib/Drupal/serialization/Plugin/EntityReferenceHandlerInterface.php @@ -17,5 +17,5 @@ * * @return int */ - public function getEntityId($data, $format, $serializer); + public function getEntityId($normalizer); } diff --git a/core/modules/serialization/lib/Drupal/serialization/Plugin/Type/EntityReferenceHandlerPluginManager.php b/core/modules/serialization/lib/Drupal/serialization/Plugin/Type/EntityReferenceHandlerPluginManager.php index 02a01f2..e33fdf3 100644 --- a/core/modules/serialization/lib/Drupal/serialization/Plugin/Type/EntityReferenceHandlerPluginManager.php +++ b/core/modules/serialization/lib/Drupal/serialization/Plugin/Type/EntityReferenceHandlerPluginManager.php @@ -23,4 +23,33 @@ public function __construct() { $this->discovery = new AnnotatedClassDiscovery('serialization', 'entityreference_handler'); $this->factory = new DefaultFactory($this); } + + /** + * Overrides \Drupal\Component\Plugin\PluginManagerBase::getInstance(). + */ + public function getInstance(array $options) { + $format = $options['format']; + $handler = $this->getHandler($format, $options['field_name']); + $config = array( + 'target_type' => $options['field']['settings']['target_type'], + ); + return $this->createInstance($handler, $config); + } + + /** + * @param $format + * The format being deserialized to. + * @param $field_name + * The field name. + * + * @return string + * The plugin id for the handler. + */ + protected function getHandler($format, $field_name) { + $entityreference_settings = entity_load('entityreference_handler_settings', $format); + if (!isset($entityreference_settings)) { + // @todo Load the default. + } + return $entityreference_settings->getHandler($field_name); + } } diff --git a/core/modules/serialization/lib/Drupal/serialization/Plugin/serialization/entityreference_handler/ConfiguredValue.php b/core/modules/serialization/lib/Drupal/serialization/Plugin/serialization/entityreference_handler/ConfiguredValue.php deleted file mode 100644 index f0b5daa..0000000 --- a/core/modules/serialization/lib/Drupal/serialization/Plugin/serialization/entityreference_handler/ConfiguredValue.php +++ /dev/null @@ -1,33 +0,0 @@ -getSetting('value'); - } -} diff --git a/core/modules/serialization/lib/Drupal/serialization/Plugin/serialization/entityreference_handler/NullValue.php b/core/modules/serialization/lib/Drupal/serialization/Plugin/serialization/entityreference_handler/NullValue.php new file mode 100644 index 0000000..b5ab78a --- /dev/null +++ b/core/modules/serialization/lib/Drupal/serialization/Plugin/serialization/entityreference_handler/NullValue.php @@ -0,0 +1,32 @@ +getSetting('uuid_key'); - $uuid = $data[$uuid_key]; - while (is_array($uuid)) { - $uuid = reset($uuid); + public function getEntityId($normalizer) { + if (($normalizer instanceof UuidReferenceInterface) && $uuid = $normalizer->getUuid()) { + $entity = entity_load_by_uuid($this->configuration['target_type'], $uuid); + return $entity->id(); } - - $entity = entity_load_by_uuid($this->getSetting('target_entity_type'), $uuid); - return $entity->id(); } } diff --git a/core/modules/serialization/lib/Drupal/serialization/Plugin/serialization/entityreference_handler/UuidReferenceInterface.php b/core/modules/serialization/lib/Drupal/serialization/Plugin/serialization/entityreference_handler/UuidReferenceInterface.php new file mode 100644 index 0000000..d4e6b5d --- /dev/null +++ b/core/modules/serialization/lib/Drupal/serialization/Plugin/serialization/entityreference_handler/UuidReferenceInterface.php @@ -0,0 +1,23 @@ + 'entity_test', - 'bundle' => 'entity_test', - 'format' => 'drupal_jsonld', - ); - $id = implode('.', $values); - $config = entity_create('entityreference_handler_settings', $values); + $format = 'drupal_jsonld'; + $config = entity_create('entityreference_handler_settings', array('format' => 'drupal_jsonld',)); $config->setHandler('user_id', 'configured_value', array('value' => 1)); $config->save(); - $loadedConfig = entity_load('entityreference_handler_settings', $id); + $loadedConfig = entity_load('entityreference_handler_settings', $format); // Test that setHandler works. $expected = array ( 'user_id' => array( - 'plugin_id' => 'configured_value', - 'settings' => array( - 'value' => 1, - ), + 'plugin_id' => 'null_value', ), ); $this->assertEqual($expected, $loadedConfig->get('entityreferenceHandlers'), 'setHandler() sets the appropriate handler.'); diff --git a/core/modules/serialization/serialization.module b/core/modules/serialization/serialization.module index 105a731..ab16759 100644 --- a/core/modules/serialization/serialization.module +++ b/core/modules/serialization/serialization.module @@ -8,31 +8,3 @@ * serialization system. */ -/** - * Get the entity reference handler settings for a bundle in a format. - * - * @param $entity_type - * The entity type of the bundle. - * @param $bundle - * The bundle. - * @param $format - * The format of the data being deserialzed. - * - * @return Drupal\serialization\Plugin\Core\Entity\EntityReferenceDeserializeSettings - * The config object. - */ -function serialization_get_entityreference_handler_settings($entity_type, $bundle, $format) { - // Try loading the settings from configuration. - $settings = entity_load('entityreference_handler_settings', $entity_type . '.' . $bundle . '.' . $format); - - // If not found, create a fresh config object. - if (!$settings) { - $settings = entity_create('entityreference_handler_settings', array( - 'targetEntityType' => $entity_type, - 'bundle' => $bundle, - 'format' => $format, - )); - } - - return $settings; -}