You are here

class PreparedAttributeNormalizer in Commerce Cart Flyout 8

Hierarchy

Expanded class hierarchy of PreparedAttributeNormalizer

1 string reference to 'PreparedAttributeNormalizer'
commerce_cart_flyout.services.yml in ./commerce_cart_flyout.services.yml
commerce_cart_flyout.services.yml
1 service uses PreparedAttributeNormalizer
commerce_cart_flyout.normalizer.prepared_attribute in ./commerce_cart_flyout.services.yml
Drupal\commerce_cart_flyout\Normalizer\PreparedAttributeNormalizer

File

src/Normalizer/PreparedAttributeNormalizer.php, line 9

Namespace

Drupal\commerce_cart_flyout\Normalizer
View 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

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
NormalizerBase::$format protected property List of formats which supports (de-)normalization. 3
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. 2
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() 1
NormalizerBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. 1
PreparedAttributeNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides NormalizerBase::$supportedInterfaceOrClass
PreparedAttributeNormalizer::normalize public function @inheritDoc