You are here

class FieldItemNormalizer in JSON:API Extras 8.3

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

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

Hierarchy

  • class \Drupal\jsonapi_extras\Normalizer\JsonApiNormalizerDecoratorBase implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface, \Symfony\Component\Serializer\Normalizer\DenormalizerInterface, \Symfony\Component\Serializer\SerializerAwareInterface

Expanded class hierarchy of FieldItemNormalizer

1 file declares its use of FieldItemNormalizer
FieldItemNormalizerImpostor.php in src-impostor-normalizers/FieldItemNormalizerImpostor.php

File

src/Normalizer/FieldItemNormalizer.php, line 15

Namespace

Drupal\jsonapi_extras\Normalizer
View source
class FieldItemNormalizer extends JsonApiNormalizerDecoratorBase {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The field enhancer manager.
   *
   * @var \Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager
   */
  protected $enhancerManager;

  /**
   * Constructs a new FieldItemNormalizer.
   *
   * @param \Drupal\jsonapi\Normalizer\FieldItemNormalizer $inner
   *   The JSON:API field normalizer entity.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager $enhancer_manager
   *   The field enhancer manager.
   */
  public function __construct(JsonapiFieldItemNormalizer $inner, EntityTypeManagerInterface $entity_type_manager, ResourceFieldEnhancerManager $enhancer_manager) {
    parent::__construct($inner);
    $this->entityTypeManager = $entity_type_manager;
    $this->enhancerManager = $enhancer_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function normalize($object, $format = NULL, array $context = []) {

    // First get the regular output.
    $normalized_output = parent::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_object']
      ->getResourceType();
    $enhancer = $resource_type
      ->getFieldEnhancer($object
      ->getParent()
      ->getName());
    if (!$enhancer) {
      return $normalized_output;
    }

    // Apply any enhancements necessary.
    $context['field_item_object'] = $object;
    $processed = $enhancer
      ->undoTransform($normalized_output
      ->getNormalization(), new Context($context));
    $cacheability = CacheableMetadata::createFromObject($normalized_output)
      ->addCacheTags([
      'config:jsonapi_resource_config_list',
    ]);
    $normalized_output = new CacheableNormalization($cacheability, $processed);
    return $normalized_output;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldItemNormalizer::$enhancerManager protected property The field enhancer manager.
FieldItemNormalizer::$entityTypeManager protected property The entity type manager.
FieldItemNormalizer::normalize public function Normalizes an object into a set of arrays/scalars. Overrides JsonApiNormalizerDecoratorBase::normalize
FieldItemNormalizer::__construct public function Constructs a new FieldItemNormalizer. Overrides JsonApiNormalizerDecoratorBase::__construct
JsonApiNormalizerDecoratorBase::$inner protected property The decorated (de)normalizer.
JsonApiNormalizerDecoratorBase::denormalize public function Denormalizes data back into an object of the given class. 2
JsonApiNormalizerDecoratorBase::setSerializer public function Sets the owning Serializer object.
JsonApiNormalizerDecoratorBase::supportsDenormalization public function Checks whether the given class is supported for denormalization by this normalizer.
JsonApiNormalizerDecoratorBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer.