class FieldNormalizer in Commerce Cart API 8
Field normalizer which flattens output.
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\commerce_cart_api\Normalizer\FieldNormalizer
- class \Drupal\serialization\Normalizer\FieldNormalizer implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
- class \Drupal\serialization\Normalizer\ListNormalizer
Expanded class hierarchy of FieldNormalizer
1 string reference to 'FieldNormalizer'
1 service uses FieldNormalizer
File
- src/
Normalizer/ FieldNormalizer.php, line 12
Namespace
Drupal\commerce_cart_api\NormalizerView source
class FieldNormalizer extends CoreFieldNormalizer {
/**
* The current route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* Constructs a new FieldNormalizer object.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
*/
public function __construct(RouteMatchInterface $route_match) {
$this->routeMatch = $route_match;
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = NULL) {
$supported = parent::supportsNormalization($data, $format);
if ($supported) {
$route = $this->routeMatch
->getRouteObject();
return $route && $route
->hasRequirement('_cart_api');
}
return $supported;
}
/**
* {@inheritdoc}
*/
public function normalize($field_item, $format = NULL, array $context = []) {
/** @var \Drupal\Core\Field\FieldItemListInterface $field_item */
$cardinality = $field_item
->getFieldDefinition()
->getFieldStorageDefinition()
->getCardinality();
$data = parent::normalize($field_item, $format, $context);
if ($cardinality > 1 || $cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
return $data;
}
if (empty($data)) {
return NULL;
}
return reset($data);
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = NULL, array $context = []) {
if (!is_array($data)) {
$data = [
$data,
];
}
return parent::denormalize($data, $class, $format, $context);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheableNormalizerInterface:: |
constant | Name of key for bubbling cacheability metadata via serialization context. | ||
FieldNormalizer:: |
protected | property | The current route match. | |
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. Overrides FieldNormalizer:: |
|
FieldNormalizer:: |
public | function |
Normalizes an object into a set of arrays/scalars. Overrides ListNormalizer:: |
|
FieldNormalizer:: |
public | function |
Checks whether the given class is supported for normalization by this normalizer. Overrides NormalizerBase:: |
|
FieldNormalizer:: |
public | function | Constructs a new FieldNormalizer object. | |
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() | 1 |