class PreparedAttributeNormalizer in Commerce Cart Flyout 8
Hierarchy
- class \Drupal\serialization\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\SerializerAwareInterface, CacheableNormalizerInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
- class \Drupal\commerce_cart_flyout\Normalizer\PreparedAttributeNormalizer
Expanded class hierarchy of PreparedAttributeNormalizer
1 string reference to 'PreparedAttributeNormalizer'
1 service uses PreparedAttributeNormalizer
File
- src/
Normalizer/ PreparedAttributeNormalizer.php, line 9
Namespace
Drupal\commerce_cart_flyout\NormalizerView source
class PreparedAttributeNormalizer extends NormalizerBase {
/**
* The interface or class that this Normalizer supports.
*
* @var string
*/
protected $supportedInterfaceOrClass = 'Drupal\\commerce_product\\PreparedAttribute';
/**
* @inheritDoc
*/
public function normalize($object, $format = NULL, array $context = []) {
if (!$object instanceof PreparedAttribute) {
return '';
}
$data = $object
->toArray();
$attribute_ids = array_keys($data['values']);
/** @var \Drupal\commerce_product\ProductAttributeValueStorageInterface $attribute_value_storage */
$attribute_value_storage = \Drupal::entityTypeManager()
->getStorage('commerce_product_attribute_value');
/** @var \Drupal\commerce_product\Entity\ProductAttributeValueInterface[] $attributes */
// @todo make sure loaded in same langcode.
$attributes = array_reduce($attribute_value_storage
->loadMultiple($attribute_ids), function ($carry, ProductAttributeValueInterface $value) {
$carry[] = $value;
return $carry;
}, []);
if ($object
->getElementType() == 'commerce_product_rendered_attribute') {
$view_builder = \Drupal::entityTypeManager()
->getViewBuilder('commerce_product_attribute_value');
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
foreach ($attributes as $key => $value) {
$rendered_attribute = $view_builder
->view($value, 'add_to_cart');
$attributes[$key]->rendered = $renderer
->render($rendered_attribute);
}
}
$data['values'] = $this->serializer
->normalize($attributes);
return $data;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheableNormalizerInterface:: |
constant | Name of key for bubbling cacheability metadata via serialization context. | ||
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 |
NormalizerBase:: |
public | function | Checks whether the given class is supported for normalization by this normalizer. | 1 |
PreparedAttributeNormalizer:: |
protected | property |
The interface or class that this Normalizer supports. Overrides NormalizerBase:: |
|
PreparedAttributeNormalizer:: |
public | function | @inheritDoc |