You are here

public function FieldNormalizer::normalize in JSON:API 8

Same name and namespace in other branches
  1. 8.2 src/Normalizer/FieldNormalizer.php \Drupal\jsonapi\Normalizer\FieldNormalizer::normalize()
1 method overrides FieldNormalizer::normalize()
EntityReferenceFieldNormalizer::normalize in src/Normalizer/EntityReferenceFieldNormalizer.php
Normalizes an object into a set of arrays/scalars.

File

src/Normalizer/FieldNormalizer.php, line 37

Class

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

Namespace

Drupal\jsonapi\Normalizer

Code

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

  /* @var \Drupal\Core\Field\FieldItemListInterface $field */
  $access = $field
    ->access('view', $context['account'], TRUE);
  $property_type = static::isRelationship($field) ? 'relationships' : 'attributes';
  if ($access
    ->isAllowed()) {
    $normalized_field_items = $this
      ->normalizeFieldItems($field, $format, $context);
    assert(Inspector::assertAll(function ($v) {
      return $v instanceof FieldItemNormalizerValue;
    }, $normalized_field_items));
    $cardinality = $field
      ->getFieldDefinition()
      ->getFieldStorageDefinition()
      ->getCardinality();
    return new FieldNormalizerValue($access, $normalized_field_items, $cardinality, $property_type);
  }
  else {
    return new NullFieldNormalizerValue($access, $property_type);
  }
}