View source
<?php
namespace Drupal\commerce_cart_flyout\Plugin\Field\FieldFormatter;
use Drupal\commerce_product\Entity\ProductInterface;
use Drupal\commerce_product\Entity\ProductVariationInterface;
use Drupal\commerce_product\PreparedAttribute;
use Drupal\commerce_product\ProductVariationAttributeMapperInterface;
use Drupal\commerce_product\ProductVariationFieldRendererInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Template\Attribute;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Serializer\Serializer;
class AddToCart extends FormatterBase implements ContainerFactoryPluginInterface {
protected $variationStorage;
protected $routeMatch;
protected $serializer;
protected $attributeMapper;
protected $renderer;
protected $attributeValueViewBuilder;
protected $attributeValueStorage;
protected $variationFieldRenderer;
protected $orderItemStorage;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager, RouteMatchInterface $route_match, Serializer $serializer, ProductVariationAttributeMapperInterface $attribute_mapper, RendererInterface $renderer, ProductVariationFieldRendererInterface $variation_field_renderer) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->variationStorage = $entity_type_manager
->getStorage('commerce_product_variation');
$this->routeMatch = $route_match;
$this->serializer = $serializer;
$this->attributeMapper = $attribute_mapper;
$this->renderer = $renderer;
$this->attributeValueViewBuilder = $entity_type_manager
->getViewBuilder('commerce_product_attribute_value');
$this->attributeValueStorage = $entity_type_manager
->getStorage('commerce_product_attribute_value');
$this->variationFieldRenderer = $variation_field_renderer;
$this->orderItemStorage = $entity_type_manager
->getStorage('commerce_order_item');
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('entity_type.manager'), $container
->get('current_route_match'), $container
->get('serializer'), $container
->get('commerce_product.variation_attribute_mapper'), $container
->get('renderer'), $container
->get('commerce_product.variation_field_renderer'));
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$product = $items
->getEntity();
$default_variation = $this->variationStorage
->loadFromContext($product);
if (!$default_variation) {
return [];
}
$order_item = $this->orderItemStorage
->createFromPurchasableEntity($default_variation);
$form_display = EntityFormDisplay::collectRenderDisplay($order_item, 'add_to_cart');
$purchased_entity_widget = $form_display
->getComponent('purchased_entity');
$variations = $this
->loadVariations($product);
$this->routeMatch
->getRouteObject()
->setRequirement('_cart_api', 'true');
$elements = [];
$elements[0]['add_to_cart_form'] = [
'#attached' => [
'library' => [
'core/drupalSettings',
'commerce_cart_flyout/add_to_cart',
],
'drupalSettings' => [
'addToCart' => [
$product
->uuid() => [
'defaultVariation' => $default_variation
->uuid(),
'variations' => $this->serializer
->normalize($variations),
'injectedFields' => $this
->getVariationInjectedFields($variations),
'type' => $purchased_entity_widget['type'],
],
],
'theme' => [
'commerce_cart_flyout_add_to_cart_button' => $this
->renderTemplate('commerce_cart_flyout_add_to_cart_button'),
'commerce_cart_flyout_add_to_cart_variation_select' => $this
->renderTemplate('commerce_cart_flyout_add_to_cart_variation_select'),
],
],
],
'#markup' => Markup::create(sprintf('<div %s></div>', new Attribute([
'data-product' => $product
->uuid(),
'data-view-mode' => $this->viewMode,
'data-langcode' => $langcode,
]))),
];
if ($purchased_entity_widget['type'] === 'commerce_product_variation_attributes') {
foreach ($variations as $variation) {
$prepared_attributes = $this->attributeMapper
->prepareAttributes($variation, $variations);
$prepared_attributes = array_filter($prepared_attributes, static function (PreparedAttribute $prepared_attribute) {
$values = $prepared_attribute
->getValues();
return count($values) > 1 || !isset($values['_none']);
});
$elements[0]['add_to_cart_form']['#attached']['library'][] = 'commerce_product/rendered-attributes';
$elements[0]['add_to_cart_form']['#attached']['drupalSettings']['addToCart'][$product
->uuid()]['attributeOptions'][$variation
->uuid()] = [
'attributes' => $this->serializer
->normalize(array_values($prepared_attributes)),
'renderedAttributes' => $this
->renderPreparedAttributes($prepared_attributes),
];
}
$elements[0]['add_to_cart_form']['#attached']['drupalSettings']['theme'] += [
'commerce_cart_flyout_add_to_cart_attributes_select' => $this
->renderTemplate('commerce_cart_flyout_add_to_cart_attributes_select'),
'commerce_cart_flyout_add_to_cart_attributes_radios' => $this
->renderTemplate('commerce_cart_flyout_add_to_cart_attributes_radios'),
'commerce_cart_flyout_add_to_cart_attributes_rendered' => $this
->renderTemplate('commerce_cart_flyout_add_to_cart_attributes_rendered'),
];
}
$cacheability = new CacheableMetadata();
$cacheability
->addCacheableDependency($order_item);
$cacheability
->addCacheableDependency($form_display);
$cacheability
->addCacheableDependency($product);
foreach ($variations as $variation) {
$cacheability
->addCacheableDependency($variation);
}
$cacheability
->applyTo($elements);
return $elements;
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
$has_cart = \Drupal::moduleHandler()
->moduleExists('commerce_cart');
$entity_type = $field_definition
->getTargetEntityTypeId();
$field_name = $field_definition
->getName();
return $has_cart && $entity_type == 'commerce_product' && $field_name == 'variations';
}
protected function filterPreparedAttributedByElementType(array $prepared_attributes, $element_type) {
return array_filter($prepared_attributes, function (PreparedAttribute $attribute) use ($element_type) {
return $attribute
->getElementType() == $element_type;
});
}
protected function renderPreparedAttributes(array $prepared_attributes) {
return array_map(function (PreparedAttribute $attribute) {
return array_map(function ($attribute_value_id) {
$attribute_value = $this->attributeValueStorage
->load($attribute_value_id);
$attribute_value_build = $this->attributeValueViewBuilder
->view($attribute_value, 'add_to_cart');
return [
'output' => $this->renderer
->render($attribute_value_build),
'attribute_value_id' => $attribute_value_id,
];
}, array_keys($attribute
->getValues()));
}, $this
->filterPreparedAttributedByElementType($prepared_attributes, 'commerce_product_rendered_attribute'));
}
protected function loadVariations(ProductInterface $product) {
return array_reduce($this->variationStorage
->loadEnabled($product), function ($carry, ProductVariationInterface $variation) {
$carry[$variation
->uuid()] = $variation;
return $carry;
}, []);
}
protected function renderTemplate($hook) {
$build = [
'#theme' => $hook,
];
return $this->renderer
->renderPlain($build);
}
protected function getVariationInjectedFields(array $variations) {
return array_map(function (ProductVariationInterface $variation) {
return array_filter(array_map(function ($build) {
return [
'class' => $build['#ajax_replace_class'],
'output' => trim($this->renderer
->render($build)),
];
}, $this->variationFieldRenderer
->renderFields($variation, $this->viewMode)), function ($built) {
return !empty($built['output']);
});
}, $variations);
}
}