You are here

class FieldItemNormalizer in JSON:API Extras 8

Same name and namespace in other branches
  1. 8.3 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

Expanded class hierarchy of FieldItemNormalizer

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

File

src/Normalizer/FieldItemNormalizer.php, line 15

Namespace

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

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

  /**
   * The JSON API field normalizer entity.
   *
   * @var \Drupal\jsonapi\Normalizer\FieldItemNormalizer
   */
  protected $subject;

  /**
   * 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 $subject
   *   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 $subject, EntityTypeManagerInterface $entity_type_manager, ResourceFieldEnhancerManager $enhancer_manager) {
    $this->subject = $subject;
    $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 = $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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
FieldItemNormalizer::$enhancerManager protected property The field enhancer manager.
FieldItemNormalizer::$entityTypeManager protected property The entity type manager.
FieldItemNormalizer::$subject protected property The JSON API field normalizer entity.
FieldItemNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides NormalizerBase::$supportedInterfaceOrClass
FieldItemNormalizer::normalize public function Normalizes an object into a set of arrays/scalars.
FieldItemNormalizer::__construct public function Constructs a new FieldItemNormalizer.
NormalizerBase::$format protected property List of formats which supports (de-)normalization. Overrides NormalizerBase::$format
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. Overrides NormalizerBase::checkFormat
NormalizerBase::rasterizeValueRecursive protected static function Rasterizes a value recursively.
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() 1
NormalizerBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. 1