src/Normalizer/EntityNormalizer.php | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/Normalizer/EntityNormalizer.php b/src/Normalizer/EntityNormalizer.php index 1ddc5ab..ec99785 100644 --- a/src/Normalizer/EntityNormalizer.php +++ b/src/Normalizer/EntityNormalizer.php @@ -129,7 +129,7 @@ class EntityNormalizer extends NormalizerBase implements DenormalizerInterface { $normalization_parts = $cached_normalization_parts->data; } else { - $normalization_parts = $this->doNormalize($resource_type, $entity, $format, $context); + $normalization_parts = $this->getPartialNormalization($resource_type, $entity, $format, $context); $normalization_parts_cacheability = static::mergeCacheableDependencies($normalization_parts['fields']); $cache_tags = Cache::mergeTags($entity->getCacheTags(), $normalization_parts_cacheability->getCacheTags()); $this->cache->set($cid, $normalization_parts, Cache::PERMANENT, $cache_tags); @@ -151,17 +151,30 @@ class EntityNormalizer extends NormalizerBase implements DenormalizerInterface { } /** - * Does the actual normalization. + * Normalizes an entity into a partial normalization. * - * @todo remove $format & $context + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity to partially normalize. + * @param \Drupal\jsonapi\ResourceType\ResourceType $resource_type + * The JSON:API resource type for the given entity. + * @param string $format + * Format the normalization result will be encoded as. + * @param array $context + * Context options for the normalizer. + * + * @return array + * An array with two key-value pairs: + * - 'base': array, the base normalization of the entity, that does not + * depend on which sparse fieldset was requested. + * - 'fields': CacheableNormalization for every field on this entity, of + * which a subset will need to be selected depending on the + * requested sparse fieldset. + * + * @see ::normalize() */ - private function doNormalize(ResourceType $resource_type, EntityInterface $entity, $format = NULL, array $context = []) { - // Get the bundle ID of the requested resource. This is used to determine if - // this is a bundle level resource or an entity level resource. - $bundle = $resource_type->getBundle(); - $field_names = $this->getFieldNames($entity, $bundle, $resource_type); + private function getPartialNormalization(EntityInterface $entity, ResourceType $resource_type, $format = NULL, array $context = []) { $normalizer_values = []; - foreach ($this->getFields($entity, $bundle, $resource_type) as $field_name => $field) { + foreach ($this->getFields($entity, $resource_type->getBundle(), $resource_type) as $field_name => $field) { $normalizer_values[$field_name] = $this->serializeField($field, $context, $format); } // Create the array of normalized fields, starting with the URI.