function EntityReferenceFieldItemNormalizer::normalize
Same name in other branches
- 9 core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizer.php \Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer::normalize()
- 10 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 42
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->getEntityTypeId() === 'file') {
$values['url'] = file_create_url($entity->getFileUri());
}
}
return $values;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.