class FieldItemNormalizer

Same name in this branch
  1. 9 core/modules/jsonapi/src/Normalizer/FieldItemNormalizer.php \Drupal\jsonapi\Normalizer\FieldItemNormalizer
  2. 9 core/modules/serialization/src/Normalizer/FieldItemNormalizer.php \Drupal\serialization\Normalizer\FieldItemNormalizer
Same name in other branches
  1. 8.9.x core/modules/jsonapi/src/Normalizer/FieldItemNormalizer.php \Drupal\jsonapi\Normalizer\FieldItemNormalizer
  2. 8.9.x core/modules/serialization/src/Normalizer/FieldItemNormalizer.php \Drupal\serialization\Normalizer\FieldItemNormalizer
  3. 8.9.x core/modules/hal/src/Normalizer/FieldItemNormalizer.php \Drupal\hal\Normalizer\FieldItemNormalizer
  4. 10 core/modules/jsonapi/src/Normalizer/FieldItemNormalizer.php \Drupal\jsonapi\Normalizer\FieldItemNormalizer
  5. 10 core/modules/serialization/src/Normalizer/FieldItemNormalizer.php \Drupal\serialization\Normalizer\FieldItemNormalizer
  6. 11.x core/modules/jsonapi/src/Normalizer/FieldItemNormalizer.php \Drupal\jsonapi\Normalizer\FieldItemNormalizer
  7. 11.x core/modules/serialization/src/Normalizer/FieldItemNormalizer.php \Drupal\serialization\Normalizer\FieldItemNormalizer

Converts the Drupal field item object structure to HAL array structure.

Hierarchy

Expanded class hierarchy of FieldItemNormalizer

1 file declares its use of FieldItemNormalizer
FieldNormalizerDenormalizeExceptionsTest.php in core/modules/hal/tests/src/Unit/FieldNormalizerDenormalizeExceptionsTest.php
1 string reference to 'FieldItemNormalizer'
hal.services.yml in core/modules/hal/hal.services.yml
core/modules/hal/hal.services.yml
1 service uses FieldItemNormalizer
serializer.normalizer.field_item.hal in core/modules/hal/hal.services.yml
Drupal\hal\Normalizer\FieldItemNormalizer

File

core/modules/hal/src/Normalizer/FieldItemNormalizer.php, line 14

Namespace

Drupal\hal\Normalizer
View source
class FieldItemNormalizer extends NormalizerBase {
    use FieldableEntityNormalizerTrait;
    use SerializedColumnNormalizerTrait;
    
    /**
     * {@inheritdoc}
     */
    protected $supportedInterfaceOrClass = FieldItemInterface::class;
    
    /**
     * {@inheritdoc}
     */
    public function normalize($field_item, $format = NULL, array $context = []) {
        // The values are wrapped in an array, and then wrapped in another array
        // keyed by field name so that field items can be merged by the
        // FieldNormalizer. This is necessary for the EntityReferenceItemNormalizer
        // to be able to place values in the '_links' array.
        $field = $field_item->getParent();
        return [
            $field->getName() => [
                $this->normalizedFieldValues($field_item, $format, $context),
            ],
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function denormalize($data, $class, $format = NULL, array $context = []) {
        if (!isset($context['target_instance'])) {
            throw new InvalidArgumentException('$context[\'target_instance\'] must be set to denormalize with the FieldItemNormalizer');
        }
        if ($context['target_instance']->getParent() == NULL) {
            throw new InvalidArgumentException('The field item passed in via $context[\'target_instance\'] must have a parent set.');
        }
        $field_item = $context['target_instance'];
        $this->checkForSerializedStrings($data, $class, $field_item);
        // If this field is translatable, we need to create a translated instance.
        if (isset($data['lang'])) {
            $langcode = $data['lang'];
            unset($data['lang']);
            $field_definition = $field_item->getFieldDefinition();
            if ($field_definition->isTranslatable()) {
                $field_item = $this->createTranslatedInstance($field_item, $langcode);
            }
        }
        $field_item->setValue($this->constructValue($data, $context));
        return $field_item;
    }
    
    /**
     * Normalizes field values for an item.
     *
     * @param \Drupal\Core\Field\FieldItemInterface $field_item
     *   The field item instance.
     * @param string|null $format
     *   The normalization format.
     * @param array $context
     *   The context passed into the normalizer.
     *
     * @return array
     *   An array of field item values, keyed by property name.
     */
    protected function normalizedFieldValues(FieldItemInterface $field_item, $format, array $context) {
        $normalized = [];
        // We normalize each individual property, so each can do their own casting,
        // if needed.
        
        /** @var \Drupal\Core\TypedData\TypedDataInterface $property */
        $field_properties = !empty($field_item->getProperties(TRUE)) ? TypedDataInternalPropertiesHelper::getNonInternalProperties($field_item) : $field_item->getValue();
        foreach ($field_properties as $property_name => $property) {
            $normalized[$property_name] = $this->serializer
                ->normalize($property, $format, $context);
        }
        if (isset($context['langcode'])) {
            $normalized['lang'] = $context['langcode'];
        }
        return $normalized;
    }
    
    /**
     * Get a translated version of the field item instance.
     *
     * To indicate that a field item applies to one translation of an entity and
     * not another, the property path must originate with a translation of the
     * entity. This is the reason for using target_instances, from which the
     * property path can be traversed up to the root.
     *
     * @param \Drupal\Core\Field\FieldItemInterface $item
     *   The untranslated field item instance.
     * @param $langcode
     *   The langcode.
     *
     * @return \Drupal\Core\Field\FieldItemInterface
     *   The translated field item instance.
     */
    protected function createTranslatedInstance(FieldItemInterface $item, $langcode) {
        // Remove the untranslated item that was created for the default language
        // by FieldNormalizer::denormalize().
        $items = $item->getParent();
        $delta = $item->getName();
        unset($items[$delta]);
        // Instead, create a new item for the entity in the requested language.
        $entity = $item->getEntity();
        $entity_translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode);
        $field_name = $item->getFieldDefinition()
            ->getName();
        return $entity_translation->get($field_name)
            ->appendItem();
    }
    
    /**
     * {@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.
FieldableEntityNormalizerTrait::$entityFieldManager protected property The entity field manager.
FieldableEntityNormalizerTrait::$entityTypeManager protected property The entity type manager. 1
FieldableEntityNormalizerTrait::$entityTypeRepository protected property The entity type repository.
FieldableEntityNormalizerTrait::constructValue protected function Build the field item value using the incoming data. 7
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::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides NormalizerBase::$supportedInterfaceOrClass 2
FieldItemNormalizer::createTranslatedInstance protected function Get a translated version of the field item instance.
FieldItemNormalizer::denormalize public function
FieldItemNormalizer::hasCacheableSupportsMethod public function Overrides NormalizerBase::hasCacheableSupportsMethod 2
FieldItemNormalizer::normalize public function 1
FieldItemNormalizer::normalizedFieldValues protected function Normalizes field values for an item. 2
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.