class FieldNormalizer in JSON:API 8
Same name and namespace in other branches
- 8.2 src/Normalizer/FieldNormalizer.php \Drupal\jsonapi\Normalizer\FieldNormalizer
Converts the Drupal field structure 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\FieldNormalizer
- class \Drupal\jsonapi\Normalizer\NormalizerBase
Expanded class hierarchy of FieldNormalizer
1 string reference to 'FieldNormalizer'
1 service uses FieldNormalizer
File
- src/
Normalizer/ FieldNormalizer.php, line 18
Namespace
Drupal\jsonapi\NormalizerView source
class FieldNormalizer extends NormalizerBase {
/**
* The interface or class that this Normalizer supports.
*
* @var string
*/
protected $supportedInterfaceOrClass = FieldItemListInterface::class;
/**
* The formats that the Normalizer can handle.
*
* @var array
*/
protected $formats = [
'api_json',
];
/**
* {@inheritdoc}
*/
public function normalize($field, $format = NULL, array $context = []) {
/* @var \Drupal\Core\Field\FieldItemListInterface $field */
$access = $field
->access('view', $context['account'], TRUE);
$property_type = static::isRelationship($field) ? 'relationships' : 'attributes';
if ($access
->isAllowed()) {
$normalized_field_items = $this
->normalizeFieldItems($field, $format, $context);
assert(Inspector::assertAll(function ($v) {
return $v instanceof FieldItemNormalizerValue;
}, $normalized_field_items));
$cardinality = $field
->getFieldDefinition()
->getFieldStorageDefinition()
->getCardinality();
return new FieldNormalizerValue($access, $normalized_field_items, $cardinality, $property_type);
}
else {
return new NullFieldNormalizerValue($access, $property_type);
}
}
/**
* Checks if the passed field is a relationship field.
*
* @param mixed $field
* The field.
*
* @return bool
* TRUE if it's a JSON API relationship.
*/
protected static function isRelationship($field) {
return $field instanceof EntityReferenceFieldItemList || $field instanceof Relationship;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = NULL, array $context = []) {
throw new UnexpectedValueException('Denormalization not implemented for JSON API');
}
/**
* Helper function to normalize field items.
*
* @param \Drupal\Core\Field\FieldItemListInterface $field
* The field object.
* @param string $format
* The format.
* @param array $context
* The context array.
*
* @return \Drupal\jsonapi\Normalizer\Value\FieldItemNormalizerValue[]
* The array of normalized field items.
*/
protected function normalizeFieldItems(FieldItemListInterface $field, $format, array $context) {
$normalizer_items = [];
if (!$field
->isEmpty()) {
foreach ($field as $field_item) {
$normalizer_items[] = $this->serializer
->normalize($field_item, $format, $context);
}
}
return $normalizer_items;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheableNormalizerInterface:: |
constant | Name of key for bubbling cacheability metadata via serialization context. | ||
FieldNormalizer:: |
protected | property |
The formats that the Normalizer can handle. Overrides NormalizerBase:: |
|
FieldNormalizer:: |
protected | property |
The interface or class that this Normalizer supports. Overrides NormalizerBase:: |
1 |
FieldNormalizer:: |
public | function | 1 | |
FieldNormalizer:: |
protected static | function | Checks if the passed field is a relationship field. | |
FieldNormalizer:: |
public | function | Normalizes an object into a set of arrays/scalars. | 1 |
FieldNormalizer:: |
protected | function | Helper function to normalize field items. | |
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:: |