annotate core/modules/serialization/src/EntityResolver/UuidResolver.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\serialization\EntityResolver;
Chris@0 4
Chris@0 5 use Drupal\Core\Entity\EntityManagerInterface;
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@0 14 * The entity manager.
Chris@0 15 *
Chris@0 16 * @var \Drupal\Core\Entity\EntityManagerInterface
Chris@0 17 */
Chris@0 18 protected $entityManager;
Chris@0 19
Chris@0 20 /**
Chris@0 21 * Constructs a UuidResolver object.
Chris@0 22 *
Chris@0 23 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
Chris@0 24 * The entity manager.
Chris@0 25 */
Chris@0 26 public function __construct(EntityManagerInterface $entity_manager) {
Chris@0 27 $this->entityManager = $entity_manager;
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@0 38 if ($entity = $this->entityManager->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 }