function ResourceObjectNormalizer::serializeField

Same name in other branches
  1. 9 core/modules/jsonapi/src/Normalizer/ResourceObjectNormalizer.php \Drupal\jsonapi\Normalizer\ResourceObjectNormalizer::serializeField()
  2. 8.9.x core/modules/jsonapi/src/Normalizer/ResourceObjectNormalizer.php \Drupal\jsonapi\Normalizer\ResourceObjectNormalizer::serializeField()
  3. 11.x 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 163

Class

ResourceObjectNormalizer
Converts the JSON:API module ResourceObject into a JSON:API array structure.

Namespace

Drupal\jsonapi\Normalizer

Code

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'] ?? NULL, 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 (is_a($context['resource_object']->getResourceType()
            ->getDeserializationTargetClass(), 'Drupal\\layout_builder\\Entity\\LayoutBuilderEntityViewDisplay', TRUE) && $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);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.