protected function ResourceObjectNormalizer::serializeField in Drupal 9
Same name and namespace in other branches
- 8 core/modules/jsonapi/src/Normalizer/ResourceObjectNormalizer.php \Drupal\jsonapi\Normalizer\ResourceObjectNormalizer::serializeField()
Serializes a given field.
Parameters
mixed $field: The field to serialize.
array $context: The normalization context.
string $format: The serialization format.
Return value
\Drupal\jsonapi\Normalizer\Value\CacheableNormalization The normalized value.
1 call to ResourceObjectNormalizer::serializeField()
- ResourceObjectNormalizer::getNormalization in core/
modules/ jsonapi/ src/ Normalizer/ ResourceObjectNormalizer.php - Normalizes an entity using the given fieldset.
File
- core/
modules/ jsonapi/ src/ Normalizer/ ResourceObjectNormalizer.php, line 172
Class
- ResourceObjectNormalizer
- Converts the JSON:API module ResourceObject into a JSON:API array structure.
Namespace
Drupal\jsonapi\NormalizerCode
protected function serializeField($field, array $context, $format) {
// Only content entities contain FieldItemListInterface fields. Since config
// entities do not have "real" fields and therefore do not have field access
// restrictions.
if ($field instanceof FieldItemListInterface) {
$field_access_result = $field
->access('view', $context['account'], TRUE);
if (!$field_access_result
->isAllowed()) {
return new CacheableOmission(CacheableMetadata::createFromObject($field_access_result));
}
if ($field instanceof EntityReferenceFieldItemListInterface) {
// Build the relationship object based on the entity reference and
// normalize that object instead.
assert(!empty($context['resource_object']) && $context['resource_object'] instanceof ResourceObject);
$resource_object = $context['resource_object'];
$relationship = Relationship::createFromEntityReferenceField($resource_object, $field);
$normalized_field = $this->serializer
->normalize($relationship, $format, $context);
}
else {
$normalized_field = $this->serializer
->normalize($field, $format, $context);
}
assert($normalized_field instanceof CacheableNormalization);
return $normalized_field
->withCacheableDependency(CacheableMetadata::createFromObject($field_access_result));
}
else {
// @todo Replace this workaround after https://www.drupal.org/node/3043245
// or remove the need for this in https://www.drupal.org/node/2942975.
// See \Drupal\layout_builder\Normalizer\LayoutEntityDisplayNormalizer.
if ($context['resource_object']
->getResourceType()
->getDeserializationTargetClass() === 'Drupal\\layout_builder\\Entity\\LayoutBuilderEntityViewDisplay' && $context['resource_object']
->getField('third_party_settings') === $field) {
unset($field['layout_builder']['sections']);
}
// Config "fields" in this case are arrays or primitives and do not need
// to be normalized.
return CacheableNormalization::permanent($field);
}
}