You are here

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

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

File

src/Normalizer/FieldItemNormalizer.php, line 64

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
    ->postProcess($normalized_output
    ->rasterizeValue());
  $normalized_output = new FieldItemNormalizerValue([
    $processed,
  ]);
  return $normalized_output;
}