3 namespace Drupal\serialization\EntityResolver;
5 use Drupal\Core\Entity\EntityRepositoryInterface;
6 use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
9 * Resolves entities from data that contains an entity UUID.
11 class UuidResolver implements EntityResolverInterface {
14 * The entity repository.
16 * @var \Drupal\Core\Entity\EntityRepositoryInterface
18 protected $entityRepository;
21 * Constructs a UuidResolver object.
23 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
24 * The entity repository.
26 public function __construct(EntityRepositoryInterface $entity_repository) {
27 $this->entityRepository = $entity_repository;
33 public function resolve(NormalizerInterface $normalizer, $data, $entity_type) {
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
36 // entity with that UUID, then return its ID.
37 if (($normalizer instanceof UuidReferenceInterface) && ($uuid = $normalizer->getUuid($data))) {
38 if ($entity = $this->entityRepository->loadEntityByUuid($entity_type, $uuid)) {