class EntityDenormalizerBase

Same name in other branches
  1. 9 core/modules/jsonapi/src/Normalizer/EntityDenormalizerBase.php \Drupal\jsonapi\Normalizer\EntityDenormalizerBase
  2. 8.9.x core/modules/jsonapi/src/Normalizer/EntityDenormalizerBase.php \Drupal\jsonapi\Normalizer\EntityDenormalizerBase
  3. 11.x core/modules/jsonapi/src/Normalizer/EntityDenormalizerBase.php \Drupal\jsonapi\Normalizer\EntityDenormalizerBase

Converts the Drupal entity object to a JSON:API array structure.

@internal JSON:API maintains no PHP API since its API is the HTTP API. This class may change at any time and this will break any dependencies on it.

Hierarchy

Expanded class hierarchy of EntityDenormalizerBase

See also

https://www.drupal.org/project/drupal/issues/3032787

jsonapi.api.php

File

core/modules/jsonapi/src/Normalizer/EntityDenormalizerBase.php, line 21

Namespace

Drupal\jsonapi\Normalizer
View source
abstract class EntityDenormalizerBase extends NormalizerBase implements DenormalizerInterface {
    
    /**
     * The JSON:API resource type repository.
     *
     * @var \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface
     */
    protected $resourceTypeRepository;
    
    /**
     * The entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * The entity field manager.
     *
     * @var \Drupal\Core\Entity\EntityFieldManagerInterface
     */
    protected $fieldManager;
    
    /**
     * The field plugin manager.
     *
     * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
     */
    protected $pluginManager;
    
    /**
     * Constructs an EntityDenormalizerBase object.
     *
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager.
     * @param \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager
     *   The entity field manager.
     * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $plugin_manager
     *   The plugin manager for fields.
     */
    public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $field_manager, FieldTypePluginManagerInterface $plugin_manager) {
        $this->entityTypeManager = $entity_type_manager;
        $this->fieldManager = $field_manager;
        $this->pluginManager = $plugin_manager;
    }
    
    /**
     * {@inheritdoc}
     */
    public function supportsNormalization($data, ?string $format = NULL, array $context = []) : bool {
        return FALSE;
    }
    
    /**
     * {@inheritdoc}
     */
    public function normalize($object, $format = NULL, array $context = []) : array|string|int|float|bool|\ArrayObject|null {
        throw new \LogicException('This method should never be called.');
    }
    
    /**
     * {@inheritdoc}
     */
    public function denormalize($data, $class, $format = NULL, array $context = []) : mixed {
        if (empty($context['resource_type']) || !$context['resource_type'] instanceof ResourceType) {
            throw new PreconditionFailedHttpException('Missing context during denormalization.');
        }
        
        /** @var \Drupal\jsonapi\ResourceType\ResourceType $resource_type */
        $resource_type = $context['resource_type'];
        $entity_type_id = $resource_type->getEntityTypeId();
        $bundle = $resource_type->getBundle();
        $bundle_key = $this->entityTypeManager
            ->getDefinition($entity_type_id)
            ->getKey('bundle');
        if ($bundle_key && $bundle) {
            $data[$bundle_key] = $bundle;
        }
        return $this->entityTypeManager
            ->getStorage($entity_type_id)
            ->create($this->prepareInput($data, $resource_type, $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.
     * @param string $format
     *   Format the given data was extracted from.
     * @param array $context
     *   Options available to the denormalizer.
     *
     * @return array
     *   The modified input data.
     */
    protected abstract function prepareInput(array $data, ResourceType $resource_type, $format, array $context);

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
EntityDenormalizerBase::$entityTypeManager protected property The entity type manager.
EntityDenormalizerBase::$fieldManager protected property The entity field manager.
EntityDenormalizerBase::$pluginManager protected property The field plugin manager.
EntityDenormalizerBase::$resourceTypeRepository protected property The JSON:API resource type repository.
EntityDenormalizerBase::denormalize public function
EntityDenormalizerBase::normalize public function
EntityDenormalizerBase::prepareInput abstract protected function Prepares the input data to create the entity. 2
EntityDenormalizerBase::supportsNormalization public function Overrides NormalizerBase::supportsNormalization
EntityDenormalizerBase::__construct public function Constructs an EntityDenormalizerBase object.
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::getSupportedTypes public function 22
NormalizerBase::hasCacheableSupportsMethod public function 16
NormalizerBase::rasterizeValueRecursive protected static function Rasterizes a value recursively.
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() 1

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.