You are here

class FieldItemNormalizer in JSON:API 8

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

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

@internal

Hierarchy

Expanded class hierarchy of FieldItemNormalizer

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

File

src/Normalizer/FieldItemNormalizer.php, line 16

Namespace

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

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

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

  /**
   * {@inheritdoc}
   *
   * This normalizer leaves JSON API normalizer land and enters the land of
   * Drupal core's serialization system. That system was never designed with
   * cacheability in mind, and hence bubbles cacheability out of band. This must
   * catch it, and pass it to the value object that JSON API uses.
   */
  public function normalize($field_item, $format = NULL, array $context = []) {

    /** @var \Drupal\Core\TypedData\TypedDataInterface $property */
    $values = [];

    // We normalize each individual property, so each can do their own casting,
    // if needed.
    // @todo Remove this when JSON API requires Drupal 8.5 or newer.
    if (floatval(\Drupal::VERSION) >= 8.5) {
      $field_item = TypedDataInternalPropertiesHelper::getNonInternalProperties($field_item);
    }

    // @todo Use the constant \Drupal\serialization\Normalizer\CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY instead of the 'cacheability' string when JSON API requires Drupal 8.5 or newer.
    $context['cacheability'] = new CacheableMetadata();
    foreach ($field_item as $property_name => $property) {
      $values[$property_name] = $this->serializer
        ->normalize($property, $format, $context);
    }
    if (isset($context['langcode'])) {
      $values['lang'] = $context['langcode'];
    }

    // @todo Use the constant \Drupal\serialization\Normalizer\CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY instead of the 'cacheability' string when JSON API requires Drupal 8.5 or newer.
    $value = new FieldItemNormalizerValue($values, $context['cacheability']);
    unset($context['cacheability']);
    return $value;
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
FieldItemNormalizer::$formats protected property The formats that the Normalizer can handle. Overrides NormalizerBase::$formats
FieldItemNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides NormalizerBase::$supportedInterfaceOrClass 1
FieldItemNormalizer::denormalize public function
FieldItemNormalizer::normalize public function This normalizer leaves JSON API normalizer land and enters the land of Drupal core's serialization system. That system was never designed with cacheability in mind, and hence bubbles cacheability out of band. This must catch it, and pass it to… 1
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