3 namespace Drupal\serialization\EntityResolver;
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
9 * Resolves entities from data that contains an entity UUID.
11 class UuidResolver implements EntityResolverInterface {
16 * @var \Drupal\Core\Entity\EntityManagerInterface
18 protected $entityManager;
21 * Constructs a UuidResolver object.
23 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
26 public function __construct(EntityManagerInterface $entity_manager) {
27 $this->entityManager = $entity_manager;
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->entityManager->loadEntityByUuid($entity_type, $uuid)) {