public function UuidResolver::resolve in Drupal 8
Same name and namespace in other branches
- 9 core/modules/serialization/src/EntityResolver/UuidResolver.php \Drupal\serialization\EntityResolver\UuidResolver::resolve()
Returns the local ID of an entity referenced by serialized data.
Drupal entities are loaded by and internally referenced by a local ID. Because different websites can use the same local ID to refer to different entities (e.g., node "1" can be a different node on foo.com and bar.com, or on example.com and staging.example.com), it is generally unsuitable for use in hypermedia data exchanges. Instead, UUIDs, URIs, or other globally unique IDs are preferred.
This function takes a $data array representing partially deserialized data for an entity reference, and resolves it to a local entity ID. For example, depending on the data specification being used, $data might contain a 'uuid' key, a 'uri' key, a 'href' key, or some other data identifying the entity, and it is up to the implementor of this interface to resolve that appropriately for the specification being used.
Parameters
\Symfony\Component\Serializer\Normalizer\NormalizerInterface $normalizer: The Normalizer which is handling the data.
array $data: The data passed into the calling Normalizer.
string $entity_type: The type of entity being resolved; e.g., 'node' or 'user'.
Return value
string|null Returns the local entity ID, if found. Otherwise, returns NULL.
Overrides EntityResolverInterface::resolve
File
- core/
modules/ serialization/ src/ EntityResolver/ UuidResolver.php, line 33
Class
- UuidResolver
- Resolves entities from data that contains an entity UUID.
Namespace
Drupal\serialization\EntityResolverCode
public function resolve(NormalizerInterface $normalizer, $data, $entity_type) {
// The normalizer is what knows the specification of the data being
// deserialized. If it can return a UUID from that data, and if there's an
// entity with that UUID, then return its ID.
if ($normalizer instanceof UuidReferenceInterface && ($uuid = $normalizer
->getUuid($data))) {
if ($entity = $this->entityRepository
->loadEntityByUuid($entity_type, $uuid)) {
return $entity
->id();
}
}
return NULL;
}