You are here

class ContentEntityDenormalizer in JSON:API Extras 8.3

Override ContentEntityNormalizer to prepare input.

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 ContentEntityDenormalizer

1 file declares its use of ContentEntityDenormalizer
ContentEntityDenormalizerImpostor.php in src-impostor-normalizers/ContentEntityDenormalizerImpostor.php

File

src/Normalizer/ContentEntityDenormalizer.php, line 10

Namespace

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

  /**
   * {@inheritdoc}
   */
  public function denormalize($data, $class, $format = NULL, array $context = []) {
    return parent::denormalize($this
      ->prepareInput($data, $context['resource_type']), $class, $format, $context);
  }

  /**
   * Prepares the input data to create the entity.
   *
   * @param array $data
   *   The input data to modify.
   * @param \Drupal\jsonapi\ResourceType\ResourceType $resource_type
   *   Contains the info about the resource type.
   *
   * @return array
   *   The modified input data.
   */
  protected function prepareInput(array $data, ResourceType $resource_type) {

    /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] $field_storage_definitions */
    $field_storage_definitions = \Drupal::service('entity_field.manager')
      ->getFieldStorageDefinitions($resource_type
      ->getEntityTypeId());
    $data_internal = [];

    /** @var \Drupal\jsonapi_extras\ResourceType\ConfigurableResourceType $resource_type */

    // Translate the public fields into the entity fields.
    foreach ($data as $public_field_name => $field_value) {

      // Skip any disabled field.
      $internal_name = $resource_type
        ->getInternalName($public_field_name);
      $entity_type_id = $resource_type
        ->getEntityTypeId();
      $entity_type_definition = \Drupal::entityTypeManager()
        ->getDefinition($entity_type_id);
      $uuid_key = $entity_type_definition
        ->getKey('uuid');
      if (!$resource_type
        ->isFieldEnabled($internal_name) && $uuid_key !== $internal_name) {
        continue;
      }
      $enhancer = $resource_type
        ->getFieldEnhancer($public_field_name, 'publicName');
      if (isset($field_storage_definitions[$internal_name])) {
        $field_storage_definition = $field_storage_definitions[$internal_name];
        if ($field_storage_definition
          ->getCardinality() === 1) {
          try {
            $field_value = $enhancer ? $enhancer
              ->transform($field_value) : $field_value;
          } catch (\TypeError $exception) {
            $field_value = NULL;
          }
        }
        elseif (is_array($field_value)) {
          foreach ($field_value as $key => $individual_field_value) {
            try {
              $field_value[$key] = $enhancer ? $enhancer
                ->transform($individual_field_value) : $individual_field_value;
            } catch (\TypeError $exception) {
              $field_value[$key] = NULL;
            }
          }
        }
      }
      $data_internal[$public_field_name] = $field_value;
    }
    return $data_internal;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentEntityDenormalizer::denormalize public function Denormalizes data back into an object of the given class. Overrides JsonApiNormalizerDecoratorBase::denormalize
ContentEntityDenormalizer::prepareInput protected function Prepares the input data to create the entity.
JsonApiNormalizerDecoratorBase::$inner protected property The decorated (de)normalizer.
JsonApiNormalizerDecoratorBase::normalize public function Normalizes an object into a set of arrays/scalars. 3
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.
JsonApiNormalizerDecoratorBase::__construct public function JsonApiNormalizerDecoratorBase constructor. 2