public function EntityReferenceFieldItemNormalizer::normalize in Drupal 9
Same name and namespace in other branches
- 8 core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizer.php \Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer::normalize()
Overrides ComplexDataNormalizer::normalize
File
- core/
modules/ serialization/ src/ Normalizer/ EntityReferenceFieldItemNormalizer.php, line 43
Class
- EntityReferenceFieldItemNormalizer
- Adds the file URI to embedded file entities.
Namespace
Drupal\serialization\NormalizerCode
public function normalize($field_item, $format = NULL, array $context = []) {
$values = parent::normalize($field_item, $format, $context);
$this
->normalizeRootReferenceValue($values, $field_item);
/** @var \Drupal\Core\Entity\EntityInterface $entity */
if ($entity = $field_item
->get('entity')
->getValue()) {
$values['target_type'] = $entity
->getEntityTypeId();
// Add the target entity UUID to the normalized output values.
$values['target_uuid'] = $entity
->uuid();
// Add a 'url' value if there is a reference and a canonical URL. Hard
// code 'canonical' here as config entities override the default $rel
// parameter value to 'edit-form.
if ($entity
->hasLinkTemplate('canonical') && !$entity
->isNew() && ($url = $entity
->toUrl('canonical')
->toString(TRUE))) {
$this
->addCacheableDependency($context, $url);
$values['url'] = $url
->getGeneratedUrl();
}
elseif ($entity instanceof FileInterface) {
$values['url'] = $entity
->createFileUrl(FALSE);
}
}
return $values;
}