class EntityReferenceItemNormalizer

Same name in other branches
  1. 8.9.x core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php \Drupal\hal\Normalizer\EntityReferenceItemNormalizer

Converts the Drupal entity reference item object to HAL array structure.

Hierarchy

Expanded class hierarchy of EntityReferenceItemNormalizer

1 string reference to 'EntityReferenceItemNormalizer'
hal.services.yml in core/modules/hal/hal.services.yml
core/modules/hal/hal.services.yml
1 service uses EntityReferenceItemNormalizer
serializer.normalizer.entity_reference_item.hal in core/modules/hal/hal.services.yml
Drupal\hal\Normalizer\EntityReferenceItemNormalizer

File

core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php, line 17

Namespace

Drupal\hal\Normalizer
View source
class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidReferenceInterface {
    use EntityReferenceFieldItemNormalizerTrait;
    
    /**
     * {@inheritdoc}
     */
    protected $supportedInterfaceOrClass = EntityReferenceItem::class;
    
    /**
     * The hypermedia link manager.
     *
     * @var \Drupal\hal\LinkManager\LinkManagerInterface
     */
    protected $linkManager;
    
    /**
     * The entity resolver.
     *
     * @var \Drupal\serialization\EntityResolver\EntityResolverInterface
     */
    protected $entityResolver;
    
    /**
     * The entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * Constructs an EntityReferenceItemNormalizer object.
     *
     * @param \Drupal\hal\LinkManager\LinkManagerInterface $link_manager
     *   The hypermedia link manager.
     * @param \Drupal\serialization\EntityResolver\EntityResolverInterface $entity_Resolver
     *   The entity resolver.
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface|null $entity_type_manager
     *   The entity type manager.
     */
    public function __construct(LinkManagerInterface $link_manager, EntityResolverInterface $entity_Resolver, EntityTypeManagerInterface $entity_type_manager = NULL) {
        $this->linkManager = $link_manager;
        $this->entityResolver = $entity_Resolver;
        $this->entityTypeManager = $entity_type_manager ?: \Drupal::service('entity_type.manager');
    }
    
    /**
     * {@inheritdoc}
     */
    public function normalize($field_item, $format = NULL, array $context = []) {
        // If this is not a fieldable entity, let the parent implementation handle
        // it, only fieldable entities are supported as embedded resources.
        if (!$this->targetEntityIsFieldable($field_item)) {
            return parent::normalize($field_item, $format, $context);
        }
        
        /** @var \Drupal\Core\Field\FieldItemInterface $field_item */
        $target_entity = $field_item->get('entity')
            ->getValue();
        // If the parent entity passed in a langcode, unset it before normalizing
        // the target entity. Otherwise, untranslatable fields of the target entity
        // will include the langcode.
        $langcode = $context['langcode'] ?? NULL;
        unset($context['langcode']);
        $context['included_fields'] = [
            'uuid',
        ];
        // Normalize the target entity.
        $embedded = $this->serializer
            ->normalize($target_entity, $format, $context);
        // @todo https://www.drupal.org/project/drupal/issues/3110815 $embedded will
        //   be NULL if the target entity does not exist. Use null coalescence
        //   operator to preserve behavior in PHP 7.4.
        $link = $embedded['_links']['self'] ?? NULL;
        // If the field is translatable, add the langcode to the link relation
        // object. This does not indicate the language of the target entity.
        if ($langcode) {
            $embedded['lang'] = $link['lang'] = $langcode;
        }
        // The returned structure will be recursively merged into the normalized
        // entity so that the items are properly added to the _links and _embedded
        // objects.
        $field_name = $field_item->getParent()
            ->getName();
        $entity = $field_item->getEntity();
        $field_uri = $this->linkManager
            ->getRelationUri($entity->getEntityTypeId(), $entity->bundle(), $field_name, $context);
        return [
            '_links' => [
                $field_uri => [
                    $link,
                ],
            ],
            '_embedded' => [
                $field_uri => [
                    $embedded,
                ],
            ],
        ];
    }
    
    /**
     * Checks whether the referenced entity is of a fieldable entity type.
     *
     * @param \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $item
     *   The reference field item whose target entity needs to be checked.
     *
     * @return bool
     *   TRUE when the referenced entity is of a fieldable entity type.
     */
    protected function targetEntityIsFieldable(EntityReferenceItem $item) {
        $target_entity = $item->get('entity')
            ->getValue();
        if ($target_entity !== NULL) {
            return $target_entity instanceof FieldableEntityInterface;
        }
        $referencing_entity = $item->getEntity();
        $target_entity_type_id = $item->getFieldDefinition()
            ->getSetting('target_type');
        // If the entity type is the same as the parent, we can check that. This is
        // just a shortcut to avoid getting the entity type definition and checking
        // the class.
        if ($target_entity_type_id === $referencing_entity->getEntityTypeId()) {
            return $referencing_entity instanceof FieldableEntityInterface;
        }
        // Otherwise, we need to get the class for the type.
        $target_entity_type = $this->entityTypeManager
            ->getDefinition($target_entity_type_id);
        $target_entity_type_class = $target_entity_type->getClass();
        return is_a($target_entity_type_class, FieldableEntityInterface::class, TRUE);
    }
    
    /**
     * {@inheritdoc}
     */
    protected function constructValue($data, $context) {
        $field_item = $context['target_instance'];
        $field_definition = $field_item->getFieldDefinition();
        $target_type = $field_definition->getSetting('target_type');
        $id = $this->entityResolver
            ->resolve($this, $data, $target_type);
        if (isset($id)) {
            return [
                'target_id' => $id,
            ] + array_intersect_key($data, $field_item->getProperties());
        }
        return NULL;
    }
    
    /**
     * {@inheritdoc}
     */
    protected function normalizedFieldValues(FieldItemInterface $field_item, $format, array $context) {
        // Normalize root reference values here so we don't need to deal with hal's
        // nested data structure for field items. This will be called from
        // \Drupal\hal\Normalizer\FieldItemNormalizer::normalize. Which will only
        // be called from this class for entities that are not fieldable.
        $normalized = parent::normalizedFieldValues($field_item, $format, $context);
        $this->normalizeRootReferenceValue($normalized, $field_item);
        return $normalized;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getUuid($data) {
        if (isset($data['uuid'])) {
            $uuid = $data['uuid'];
            // The value may be a nested array like $uuid[0]['value'].
            if (is_array($uuid) && isset($uuid[0]['value'])) {
                $uuid = $uuid[0]['value'];
            }
            return $uuid;
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function hasCacheableSupportsMethod() : bool {
        return TRUE;
    }

}

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.
EntityReferenceFieldItemNormalizerTrait::fieldItemReferencesTaxonomyTerm protected function Determines if a field item references a taxonomy term.
EntityReferenceFieldItemNormalizerTrait::normalizeRootReferenceValue protected function
EntityReferenceItemNormalizer::$entityResolver protected property The entity resolver.
EntityReferenceItemNormalizer::$entityTypeManager protected property The entity type manager. Overrides FieldableEntityNormalizerTrait::$entityTypeManager
EntityReferenceItemNormalizer::$linkManager protected property The hypermedia link manager.
EntityReferenceItemNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides FieldItemNormalizer::$supportedInterfaceOrClass
EntityReferenceItemNormalizer::constructValue protected function Build the field item value using the incoming data. Overrides FieldableEntityNormalizerTrait::constructValue
EntityReferenceItemNormalizer::getUuid public function Get the uuid from the data array. Overrides UuidReferenceInterface::getUuid
EntityReferenceItemNormalizer::hasCacheableSupportsMethod public function Overrides FieldItemNormalizer::hasCacheableSupportsMethod
EntityReferenceItemNormalizer::normalize public function Overrides FieldItemNormalizer::normalize
EntityReferenceItemNormalizer::normalizedFieldValues protected function Normalizes field values for an item. Overrides FieldItemNormalizer::normalizedFieldValues
EntityReferenceItemNormalizer::targetEntityIsFieldable protected function Checks whether the referenced entity is of a fieldable entity type.
EntityReferenceItemNormalizer::__construct public function Constructs an EntityReferenceItemNormalizer object.
FieldableEntityNormalizerTrait::$entityFieldManager protected property The entity field manager.
FieldableEntityNormalizerTrait::$entityTypeRepository protected property The entity type repository.
FieldableEntityNormalizerTrait::denormalizeFieldData protected function Denormalizes entity data by denormalizing each field individually.
FieldableEntityNormalizerTrait::determineEntityTypeId protected function Determines the entity type ID to denormalize as.
FieldableEntityNormalizerTrait::extractBundleData protected function Denormalizes the bundle property so entity creation can use it.
FieldableEntityNormalizerTrait::getEntityFieldManager protected function Returns the entity field manager.
FieldableEntityNormalizerTrait::getEntityTypeDefinition protected function Gets the entity type definition.
FieldableEntityNormalizerTrait::getEntityTypeManager protected function Returns the entity type manager.
FieldableEntityNormalizerTrait::getEntityTypeRepository protected function Returns the entity type repository.
FieldItemNormalizer::createTranslatedInstance protected function Get a translated version of the field item instance.
FieldItemNormalizer::denormalize public function
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. 1
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() 1
NormalizerBase::supportsNormalization public function 1
SerializedColumnNormalizerTrait::checkForSerializedStrings protected function Checks if there is a serialized string for a column.
SerializedColumnNormalizerTrait::dataHasStringForSerializeColumn protected function Checks if the data contains string value for serialize column.
SerializedColumnNormalizerTrait::getCustomSerializedPropertyNames protected function Gets the names of all properties the plugin treats as serialized data.
SerializedColumnNormalizerTrait::getSerializedPropertyNames protected function Gets the names of all serialized properties.

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