protected function UuidLinkEnhancer::doTransform in JSON:API Extras 8.2
Same name and namespace in other branches
- 8.3 src/Plugin/jsonapi/FieldEnhancer/UuidLinkEnhancer.php \Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer\UuidLinkEnhancer::doTransform()
File
- src/
Plugin/ jsonapi/ FieldEnhancer/ UuidLinkEnhancer.php, line 80
Class
- UuidLinkEnhancer
- Use UUID for internal link field value.
Namespace
Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancerCode
protected function doTransform($value, Context $context) {
if (isset($value['uri'])) {
// Check if it is a link to an entity.
preg_match("/entity:(.*)\\/(.*)\\/(.*)/", $value['uri'], $parsed_uri);
if (!empty($parsed_uri)) {
$entity_type = $parsed_uri[1];
$entity_uuid = $parsed_uri[3];
$entities = $this->entityTypeManager
->getStorage($entity_type)
->loadByProperties([
'uuid' => $entity_uuid,
]);
if (!empty($entities)) {
$entity = array_shift($entities);
$value['uri'] = 'entity:' . $entity_type . '/' . $entity
->id();
}
else {
// If the entity has not been imported yet we unset the field value.
$value = [];
}
}
}
return $value;
}