class FieldItemNormalizer in JSON:API 8
Same name and namespace in other branches
- 8.2 src/Normalizer/FieldItemNormalizer.php \Drupal\jsonapi\Normalizer\FieldItemNormalizer
Converts the Drupal field item object to a JSON API array structure.
@internal
Hierarchy
- class \Drupal\serialization\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\SerializerAwareInterface, CacheableNormalizerInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
- class \Drupal\jsonapi\Normalizer\NormalizerBase
- class \Drupal\jsonapi\Normalizer\FieldItemNormalizer
- class \Drupal\jsonapi\Normalizer\NormalizerBase
Expanded class hierarchy of FieldItemNormalizer
1 string reference to 'FieldItemNormalizer'
1 service uses FieldItemNormalizer
File
- src/
Normalizer/ FieldItemNormalizer.php, line 16
Namespace
Drupal\jsonapi\NormalizerView source
class FieldItemNormalizer extends NormalizerBase {
/**
* The interface or class that this Normalizer supports.
*
* @var string
*/
protected $supportedInterfaceOrClass = FieldItemInterface::class;
/**
* The formats that the Normalizer can handle.
*
* @var array
*/
protected $formats = [
'api_json',
];
/**
* {@inheritdoc}
*
* This normalizer leaves JSON API normalizer land and enters the land of
* Drupal core's serialization system. That system was never designed with
* cacheability in mind, and hence bubbles cacheability out of band. This must
* catch it, and pass it to the value object that JSON API uses.
*/
public function normalize($field_item, $format = NULL, array $context = []) {
/** @var \Drupal\Core\TypedData\TypedDataInterface $property */
$values = [];
// We normalize each individual property, so each can do their own casting,
// if needed.
// @todo Remove this when JSON API requires Drupal 8.5 or newer.
if (floatval(\Drupal::VERSION) >= 8.5) {
$field_item = TypedDataInternalPropertiesHelper::getNonInternalProperties($field_item);
}
// @todo Use the constant \Drupal\serialization\Normalizer\CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY instead of the 'cacheability' string when JSON API requires Drupal 8.5 or newer.
$context['cacheability'] = new CacheableMetadata();
foreach ($field_item as $property_name => $property) {
$values[$property_name] = $this->serializer
->normalize($property, $format, $context);
}
if (isset($context['langcode'])) {
$values['lang'] = $context['langcode'];
}
// @todo Use the constant \Drupal\serialization\Normalizer\CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY instead of the 'cacheability' string when JSON API requires Drupal 8.5 or newer.
$value = new FieldItemNormalizerValue($values, $context['cacheability']);
unset($context['cacheability']);
return $value;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = NULL, array $context = []) {
throw new UnexpectedValueException('Denormalization not implemented for JSON API');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheableNormalizerInterface:: |
constant | Name of key for bubbling cacheability metadata via serialization context. | ||
FieldItemNormalizer:: |
protected | property |
The formats that the Normalizer can handle. Overrides NormalizerBase:: |
|
FieldItemNormalizer:: |
protected | property |
The interface or class that this Normalizer supports. Overrides NormalizerBase:: |
1 |
FieldItemNormalizer:: |
public | function | ||
FieldItemNormalizer:: |
public | function | This normalizer leaves JSON API normalizer land and enters the land of Drupal core's serialization system. That system was never designed with cacheability in mind, and hence bubbles cacheability out of band. This must catch it, and pass it to… | 1 |
NormalizerBase:: |
protected | property | List of formats which supports (de-)normalization. | 3 |
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() Overrides NormalizerBase:: |
|
NormalizerBase:: |
public | function |
Checks whether the given class is supported for normalization by this normalizer. Overrides NormalizerBase:: |