class FieldNormalizer in Drupal 8
Same name in this branch
- 8 core/modules/jsonapi/src/Normalizer/FieldNormalizer.php \Drupal\jsonapi\Normalizer\FieldNormalizer
- 8 core/modules/hal/src/Normalizer/FieldNormalizer.php \Drupal\hal\Normalizer\FieldNormalizer
- 8 core/modules/serialization/src/Normalizer/FieldNormalizer.php \Drupal\serialization\Normalizer\FieldNormalizer
Same name and namespace in other branches
- 9 core/modules/hal/src/Normalizer/FieldNormalizer.php \Drupal\hal\Normalizer\FieldNormalizer
Converts the Drupal field structure to HAL array structure.
Hierarchy
- class \Drupal\serialization\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\SerializerAwareInterface, CacheableNormalizerInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
- class \Drupal\serialization\Normalizer\ListNormalizer
- class \Drupal\serialization\Normalizer\FieldNormalizer implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
- class \Drupal\hal\Normalizer\FieldNormalizer
- class \Drupal\serialization\Normalizer\FieldNormalizer implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
- class \Drupal\serialization\Normalizer\ListNormalizer
Expanded class hierarchy of FieldNormalizer
1 file declares its use of FieldNormalizer
- FieldNormalizerDenormalizeExceptionsUnitTest.php in core/
modules/ hal/ tests/ src/ Unit/ FieldNormalizerDenormalizeExceptionsUnitTest.php
1 string reference to 'FieldNormalizer'
- hal.services.yml in core/
modules/ hal/ hal.services.yml - core/modules/hal/hal.services.yml
1 service uses FieldNormalizer
- serializer.normalizer.field.hal in core/
modules/ hal/ hal.services.yml - Drupal\hal\Normalizer\FieldNormalizer
File
- core/
modules/ hal/ src/ Normalizer/ FieldNormalizer.php, line 11
Namespace
Drupal\hal\NormalizerView source
class FieldNormalizer extends SerializationFieldNormalizer {
/**
* {@inheritdoc}
*/
protected $format = [
'hal_json',
];
/**
* {@inheritdoc}
*/
public function normalize($field_items, $format = NULL, array $context = []) {
$normalized_field_items = [];
// Get the field definition.
$entity = $field_items
->getEntity();
$field_name = $field_items
->getName();
$field_definition = $field_items
->getFieldDefinition();
// If this field is not translatable, it can simply be normalized without
// separating it into different translations.
if (!$field_definition
->isTranslatable()) {
$normalized_field_items = $this
->normalizeFieldItems($field_items, $format, $context);
}
else {
foreach ($entity
->getTranslationLanguages() as $language) {
$context['langcode'] = $language
->getId();
$translation = $entity
->getTranslation($language
->getId());
$translated_field_items = $translation
->get($field_name);
$normalized_field_items = array_merge($normalized_field_items, $this
->normalizeFieldItems($translated_field_items, $format, $context));
}
}
// Merge deep so that links set in entity reference normalizers are merged
// into the links property.
return NestedArray::mergeDeepArray($normalized_field_items);
}
/**
* Helper function to normalize field items.
*
* @param \Drupal\Core\Field\FieldItemListInterface $field_items
* The field item list object.
* @param string $format
* The format.
* @param array $context
* The context array.
*
* @return array
* The array of normalized field items.
*/
protected function normalizeFieldItems($field_items, $format, $context) {
$normalized_field_items = [];
if (!$field_items
->isEmpty()) {
foreach ($field_items as $field_item) {
$normalized_field_items[] = $this->serializer
->normalize($field_item, $format, $context);
}
}
return $normalized_field_items;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheableNormalizerInterface:: |
constant | Name of key for bubbling cacheability metadata via serialization context. | ||
FieldNormalizer:: |
protected | property |
List of formats which supports (de-)normalization. Overrides NormalizerBase:: |
|
FieldNormalizer:: |
protected | property |
The interface or class that this Normalizer supports. Overrides ListNormalizer:: |
|
FieldNormalizer:: |
public | function | Denormalizes data back into an object of the given class. | |
FieldNormalizer:: |
public | function |
Normalizes an object into a set of arrays/scalars. Overrides ListNormalizer:: |
|
FieldNormalizer:: |
protected | function | Helper function to normalize field items. | |
NormalizerBase:: |
protected | function | Adds cacheability if applicable. | |
NormalizerBase:: |
protected | function | Checks if the provided format is supported by this normalizer. | 2 |
NormalizerBase:: |
public | function | Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() | 1 |
NormalizerBase:: |
public | function | Checks whether the given class is supported for normalization by this normalizer. | 1 |