comparison core/modules/serialization/src/EntityResolver/UuidResolver.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
1 <?php 1 <?php
2 2
3 namespace Drupal\serialization\EntityResolver; 3 namespace Drupal\serialization\EntityResolver;
4 4
5 use Drupal\Core\Entity\EntityManagerInterface; 5 use Drupal\Core\Entity\EntityRepositoryInterface;
6 use Symfony\Component\Serializer\Normalizer\NormalizerInterface; 6 use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
7 7
8 /** 8 /**
9 * Resolves entities from data that contains an entity UUID. 9 * Resolves entities from data that contains an entity UUID.
10 */ 10 */
11 class UuidResolver implements EntityResolverInterface { 11 class UuidResolver implements EntityResolverInterface {
12 12
13 /** 13 /**
14 * The entity manager. 14 * The entity repository.
15 * 15 *
16 * @var \Drupal\Core\Entity\EntityManagerInterface 16 * @var \Drupal\Core\Entity\EntityRepositoryInterface
17 */ 17 */
18 protected $entityManager; 18 protected $entityRepository;
19 19
20 /** 20 /**
21 * Constructs a UuidResolver object. 21 * Constructs a UuidResolver object.
22 * 22 *
23 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager 23 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
24 * The entity manager. 24 * The entity repository.
25 */ 25 */
26 public function __construct(EntityManagerInterface $entity_manager) { 26 public function __construct(EntityRepositoryInterface $entity_repository) {
27 $this->entityManager = $entity_manager; 27 $this->entityRepository = $entity_repository;
28 } 28 }
29 29
30 /** 30 /**
31 * {@inheritdoc} 31 * {@inheritdoc}
32 */ 32 */
33 public function resolve(NormalizerInterface $normalizer, $data, $entity_type) { 33 public function resolve(NormalizerInterface $normalizer, $data, $entity_type) {
34 // The normalizer is what knows the specification of the data being 34 // The normalizer is what knows the specification of the data being
35 // deserialized. If it can return a UUID from that data, and if there's an 35 // deserialized. If it can return a UUID from that data, and if there's an
36 // entity with that UUID, then return its ID. 36 // entity with that UUID, then return its ID.
37 if (($normalizer instanceof UuidReferenceInterface) && ($uuid = $normalizer->getUuid($data))) { 37 if (($normalizer instanceof UuidReferenceInterface) && ($uuid = $normalizer->getUuid($data))) {
38 if ($entity = $this->entityManager->loadEntityByUuid($entity_type, $uuid)) { 38 if ($entity = $this->entityRepository->loadEntityByUuid($entity_type, $uuid)) {
39 return $entity->id(); 39 return $entity->id();
40 } 40 }
41 } 41 }
42 return NULL; 42 return NULL;
43 } 43 }