diff -u b/core/modules/hal/lib/Drupal/hal/Normalizer/EntityReferenceItemNormalizer.php b/core/modules/hal/lib/Drupal/hal/Normalizer/EntityReferenceItemNormalizer.php --- b/core/modules/hal/lib/Drupal/hal/Normalizer/EntityReferenceItemNormalizer.php +++ b/core/modules/hal/lib/Drupal/hal/Normalizer/EntityReferenceItemNormalizer.php @@ -66,7 +66,10 @@ $field_item = $context['target_instance']; $field_definition = $field_item->getDefinition(); $target_type = $field_definition['settings']['target_type']; - return $this->entityResolver->resolve($this, $data, $target_type); + if ($id = $this->entityResolver->resolve($this, $data, $target_type)) { + return array('target_id' => $id); + } + return NULL; } /** diff -u b/core/modules/serialization/lib/Drupal/serialization/EntityResolver/ChainEntityResolver.php b/core/modules/serialization/lib/Drupal/serialization/EntityResolver/ChainEntityResolver.php --- b/core/modules/serialization/lib/Drupal/serialization/EntityResolver/ChainEntityResolver.php +++ b/core/modules/serialization/lib/Drupal/serialization/EntityResolver/ChainEntityResolver.php @@ -34,9 +34,9 @@ /** * Implements \Drupal\serialization\EntityResolver\EntityResolverInterface::resolve(). */ - public function resolve(NormalizerInterface $normalizer, $data, $target_type) { + public function resolve(NormalizerInterface $normalizer, $data, $entity_type) { foreach ($this->resolvers as $resolver) { - if ($resolved = $resolver->resolve($normalizer, $data, $target_type)) { + if ($resolved = $resolver->resolve($normalizer, $data, $entity_type)) { return $resolved; } } diff -u b/core/modules/serialization/lib/Drupal/serialization/EntityResolver/EntityResolverInterface.php b/core/modules/serialization/lib/Drupal/serialization/EntityResolver/EntityResolverInterface.php --- b/core/modules/serialization/lib/Drupal/serialization/EntityResolver/EntityResolverInterface.php +++ b/core/modules/serialization/lib/Drupal/serialization/EntityResolver/EntityResolverInterface.php @@ -13,22 +13,31 @@ /** - * Resolve an ID to the corresponding entity. + * Returns the local ID of an entity referenced by serialized data. * - * If the local id is found, the target_id property should be set in the - * returned array. Otherwise, if the EntityResolver knows how to dereference - * the value, the dereferencer should be set. + * Drupal entities are loaded by and internally referenced by a local ID. + * Because different websites can use the same local ID to refer to different + * entities (e.g., node "1" can be a different node on foo.com and bar.com, or + * on example.com and staging.example.com), it is generally unsuitable for use + * in hypermedia data exchanges. Instead, UUIDs, URIs, or other globally + * unique IDs are preferred. + * + * This function takes a $data array representing partially deserialized data + * for an entity reference, and resolves it to a local entity ID. For example, + * depending on the data specification being used, $data might contain a + * 'uuid' key, a 'uri' key, a 'href' key, or some other data identifying the + * entity, and it is up to the implementor of this interface to resolve that + * appropriately for the specification being used. * * @param \Symfony\Component\Serializer\Normalizer\NormalizerInterface $normalizer * The Normalizer which is handling the data. * @param array $data * The data passed into the calling Normalizer. - * @param string $target_type - * The target type of the entity reference field. + * @param string $entity_type + * The type of entity being resolved; e.g., 'node' or 'user'. * - * @return array|NULL - * Returns an array containing the target_id, if found. Otherwise, return - * NULL. + * @return string|NULL + * Returns the local entity ID, if found. Otherwise, returns NULL. */ - public function resolve(NormalizerInterface $normalizer, $data, $target_type); + public function resolve(NormalizerInterface $normalizer, $data, $entity_type); } diff -u b/core/modules/serialization/lib/Drupal/serialization/EntityResolver/UuidResolver.php b/core/modules/serialization/lib/Drupal/serialization/EntityResolver/UuidResolver.php --- b/core/modules/serialization/lib/Drupal/serialization/EntityResolver/UuidResolver.php +++ b/core/modules/serialization/lib/Drupal/serialization/EntityResolver/UuidResolver.php @@ -9,16 +9,21 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface; +/** + * Resolves entities from data that contains an entity UUID. + */ class UuidResolver implements EntityResolverInterface { /** * Implements \Drupal\serialization\EntityResolver\EntityResolverInterface::resolve(). */ - public function resolve(NormalizerInterface $normalizer, $data, $target_type) { + public function resolve(NormalizerInterface $normalizer, $data, $entity_type) { + // The normalizer is what knows the specification of the data being + // deserialized. If it can return a UUID from that data, and if there's an + // entity with that UUID, then return its ID. if (($normalizer instanceof UuidReferenceInterface) && $uuid = $normalizer->getUuid($data)) { - // If an entity with this UUID exists on the site, return the local id. - if ($entity = entity_load_by_uuid($target_type, $uuid)) { - return array('target_id' => $entity->id()); + if ($entity = entity_load_by_uuid($entity_type, $uuid)) { + return $entity->id(); } } return NULL;