function EntityReferenceFieldItemNormalizer::normalize
Same name in other branches
- 9 core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizer.php \Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer::normalize()
- 8.9.x core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizer.php \Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer::normalize()
- 11.x core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizer.php \Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer::normalize()
Overrides ComplexDataNormalizer::normalize
File
-
core/
modules/ serialization/ src/ Normalizer/ EntityReferenceFieldItemNormalizer.php, line 38
Class
- EntityReferenceFieldItemNormalizer
- Adds the file URI to embedded file entities.
Namespace
Drupal\serialization\NormalizerCode
public function normalize($field_item, $format = NULL, array $context = []) : array|string|int|float|bool|\ArrayObject|null {
$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;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.