You are here

class FieldNormalizer in JSON:API 8

Same name and namespace in other branches
  1. 8.2 src/Normalizer/FieldNormalizer.php \Drupal\jsonapi\Normalizer\FieldNormalizer

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

@internal

Hierarchy

Expanded class hierarchy of FieldNormalizer

1 string reference to 'FieldNormalizer'
jsonapi.services.yml in ./jsonapi.services.yml
jsonapi.services.yml
1 service uses FieldNormalizer
serializer.normalizer.field.jsonapi in ./jsonapi.services.yml
Drupal\jsonapi\Normalizer\FieldNormalizer

File

src/Normalizer/FieldNormalizer.php, line 18

Namespace

Drupal\jsonapi\Normalizer
View source
class FieldNormalizer extends NormalizerBase {

  /**
   * The interface or class that this Normalizer supports.
   *
   * @var string
   */
  protected $supportedInterfaceOrClass = FieldItemListInterface::class;

  /**
   * The formats that the Normalizer can handle.
   *
   * @var array
   */
  protected $formats = [
    'api_json',
  ];

  /**
   * {@inheritdoc}
   */
  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);
    }
  }

  /**
   * Checks if the passed field is a relationship field.
   *
   * @param mixed $field
   *   The field.
   *
   * @return bool
   *   TRUE if it's a JSON API relationship.
   */
  protected static function isRelationship($field) {
    return $field instanceof EntityReferenceFieldItemList || $field instanceof Relationship;
  }

  /**
   * {@inheritdoc}
   */
  public function denormalize($data, $class, $format = NULL, array $context = []) {
    throw new UnexpectedValueException('Denormalization not implemented for JSON API');
  }

  /**
   * Helper function to normalize field items.
   *
   * @param \Drupal\Core\Field\FieldItemListInterface $field
   *   The field object.
   * @param string $format
   *   The format.
   * @param array $context
   *   The context array.
   *
   * @return \Drupal\jsonapi\Normalizer\Value\FieldItemNormalizerValue[]
   *   The array of normalized field items.
   */
  protected function normalizeFieldItems(FieldItemListInterface $field, $format, array $context) {
    $normalizer_items = [];
    if (!$field
      ->isEmpty()) {
      foreach ($field as $field_item) {
        $normalizer_items[] = $this->serializer
          ->normalize($field_item, $format, $context);
      }
    }
    return $normalizer_items;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
FieldNormalizer::$formats protected property The formats that the Normalizer can handle. Overrides NormalizerBase::$formats
FieldNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides NormalizerBase::$supportedInterfaceOrClass 1
FieldNormalizer::denormalize public function 1
FieldNormalizer::isRelationship protected static function Checks if the passed field is a relationship field.
FieldNormalizer::normalize public function Normalizes an object into a set of arrays/scalars. 1
FieldNormalizer::normalizeFieldItems protected function Helper function to normalize field items.
NormalizerBase::$format protected property List of formats which supports (de-)normalization. 3
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. 2
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() Overrides NormalizerBase::supportsDenormalization
NormalizerBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. Overrides NormalizerBase::supportsNormalization