You are here

public function FieldItemNormalizer::normalize in JSON:API Extras 8.2

Same name and namespace in other branches
  1. 8.3 src/Normalizer/FieldItemNormalizer.php \Drupal\jsonapi_extras\Normalizer\FieldItemNormalizer::normalize()
  2. 8 src/Normalizer/FieldItemNormalizer.php \Drupal\jsonapi_extras\Normalizer\FieldItemNormalizer::normalize()

File

src/Normalizer/FieldItemNormalizer.php, line 67

Class

FieldItemNormalizer
Converts the Drupal field structure to a JSON API array structure.

Namespace

Drupal\jsonapi_extras\Normalizer

Code

public function normalize($object, $format = NULL, array $context = []) {

  // First get the regular output.
  $normalized_output = $this->subject
    ->normalize($object, $format, $context);

  // Then detect if there is any enhancer to be applied here.

  /** @var \Drupal\jsonapi_extras\ResourceType\ConfigurableResourceType $resource_type */
  $resource_type = $context['resource_type'];
  $enhancer = $resource_type
    ->getFieldEnhancer($object
    ->getParent()
    ->getName());
  if (!$enhancer) {
    return $normalized_output;
  }

  // Apply any enhancements necessary.
  $processed = $enhancer
    ->undoTransform($normalized_output
    ->rasterizeValue());
  $normalized_output = new FieldItemNormalizerValue([
    $processed,
  ], new CacheableMetadata());
  return $normalized_output;
}