annotate core/modules/serialization/src/EntityResolver/UuidResolver.php @ 19:fa3358dc1485 tip

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