class AddToCart in Commerce Cart Flyout 8
Plugin implementation of the 'commerce_cart_flyout_add_to_cart' formatter.
Plugin annotation
@FieldFormatter(
id = "commerce_cart_flyout_add_to_cart",
label = @Translation("Flyout add to cart form"),
field_types = {
"entity_reference",
},
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Field\FormatterBase implements FormatterInterface, ContainerFactoryPluginInterface
- class \Drupal\commerce_cart_flyout\Plugin\Field\FieldFormatter\AddToCart implements ContainerFactoryPluginInterface
- class \Drupal\Core\Field\FormatterBase implements FormatterInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Field\PluginSettingsBase implements DependentPluginInterface, PluginSettingsInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of AddToCart
1 file declares its use of AddToCart
File
- src/
Plugin/ Field/ FieldFormatter/ AddToCart.php, line 36
Namespace
Drupal\commerce_cart_flyout\Plugin\Field\FieldFormatterView source
class AddToCart extends FormatterBase implements ContainerFactoryPluginInterface {
/**
* The variation storage.
*
* @var \Drupal\commerce_product\ProductVariationStorageInterface
*/
protected $variationStorage;
/**
* The route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* The serialize.
*
* @var \Symfony\Component\Serializer\Serializer
*/
protected $serializer;
/**
* The attribute mapper.
*
* @var \Drupal\commerce_product\ProductVariationAttributeMapperInterface
*/
protected $attributeMapper;
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* The attribute value view builder.
*
* @var \Drupal\Core\Entity\EntityViewBuilderInterface
*/
protected $attributeValueViewBuilder;
/**
* The attribute value storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $attributeValueStorage;
/**
* The variation field renderer.
*
* @var \Drupal\commerce_product\ProductVariationFieldRendererInterface
*/
protected $variationFieldRenderer;
/**
* The order item storage.
*
* @var \Drupal\commerce_order\OrderItemStorageInterface
*/
protected $orderItemStorage;
/**
* Constructs a new AddToCart object.
*
* @param string $plugin_id
* The plugin_id for the formatter.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the formatter is associated.
* @param array $settings
* The formatter settings.
* @param string $label
* The formatter label display setting.
* @param string $view_mode
* The view mode.
* @param array $third_party_settings
* Any third party settings.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
* @param \Symfony\Component\Serializer\Serializer $serializer
* The serializer.
* @param \Drupal\commerce_product\ProductVariationAttributeMapperInterface $attribute_mapper
* The attribute mapper.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
* @param \Drupal\commerce_product\ProductVariationFieldRendererInterface $variation_field_renderer
* The variation field renderer.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
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');
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
/** @var \Drupal\commerce_product\Entity\ProductInterface $product */
$product = $items
->getEntity();
// If we could not load a default variation, just bail.
$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);
// Fake a requirement on the current route so that our Normalizers run.
$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) {
// There will always be at least one value, possibly `_none`.
// If we have more than one value, allow the prepared attribute. But if
// we only have one, do not consider it, if it is the `_none` value.
$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;
}
/**
* {@inheritdoc}
*/
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';
}
/**
* Filters prepared attributes by element type.
*
* @param array $prepared_attributes
* The prepared attributes.
* @param string $element_type
* The element type to filter on.
*
* @return array
* An array of filter prepared attributes.
*/
protected function filterPreparedAttributedByElementType(array $prepared_attributes, $element_type) {
return array_filter($prepared_attributes, function (PreparedAttribute $attribute) use ($element_type) {
return $attribute
->getElementType() == $element_type;
});
}
/**
* Renders the prepared attributes.
*
* @param array $prepared_attributes
* The prepared attributed.
*
* @return array
* The array of rendered prepated attributes.
*/
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'));
}
/**
* Load variations for the product.
*
* @param \Drupal\commerce_product\Entity\ProductInterface $product
* The product.
*
* @return \Drupal\commerce_product\Entity\ProductVariationInterface[]
* The variations keyed by UUID.
*/
protected function loadVariations(ProductInterface $product) {
return array_reduce($this->variationStorage
->loadEnabled($product), function ($carry, ProductVariationInterface $variation) {
$carry[$variation
->uuid()] = $variation;
return $carry;
}, []);
}
/**
* Renders a template.
*
* @param string $hook
* The theme hook.
*
* @return string
* The rendered template.
*/
protected function renderTemplate($hook) {
$build = [
'#theme' => $hook,
];
return $this->renderer
->renderPlain($build);
}
/**
* Get injected variation fields.
*
* @param \Drupal\commerce_product\Entity\ProductVariationInterface[] $variations
* The variations.
*
* @return array
* The array of injected variation fields.
*/
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);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AddToCart:: |
protected | property | The attribute mapper. | |
AddToCart:: |
protected | property | The attribute value storage. | |
AddToCart:: |
protected | property | The attribute value view builder. | |
AddToCart:: |
protected | property | The order item storage. | |
AddToCart:: |
protected | property | The renderer. | |
AddToCart:: |
protected | property | The route match. | |
AddToCart:: |
protected | property | The serialize. | |
AddToCart:: |
protected | property | The variation field renderer. | |
AddToCart:: |
protected | property | The variation storage. | |
AddToCart:: |
public static | function |
Creates an instance of the plugin. Overrides FormatterBase:: |
|
AddToCart:: |
protected | function | Filters prepared attributes by element type. | |
AddToCart:: |
protected | function | Get injected variation fields. | |
AddToCart:: |
public static | function |
Returns if the formatter can be used for the provided field. Overrides FormatterBase:: |
|
AddToCart:: |
protected | function | Load variations for the product. | |
AddToCart:: |
protected | function | Renders the prepared attributes. | |
AddToCart:: |
protected | function | Renders a template. | |
AddToCart:: |
public | function |
Builds a renderable array for a field value. Overrides FormatterInterface:: |
|
AddToCart:: |
public | function |
Constructs a new AddToCart object. Overrides FormatterBase:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormatterBase:: |
protected | property | The field definition. | |
FormatterBase:: |
protected | property | The label display setting. | |
FormatterBase:: |
protected | property |
The formatter settings. Overrides PluginSettingsBase:: |
|
FormatterBase:: |
protected | property | The view mode. | |
FormatterBase:: |
protected | function | Returns the value of a field setting. | |
FormatterBase:: |
protected | function | Returns the array of field settings. | |
FormatterBase:: |
public | function |
Allows formatters to load information for field values being displayed. Overrides FormatterInterface:: |
2 |
FormatterBase:: |
public | function |
Returns a form to configure settings for the formatter. Overrides FormatterInterface:: |
24 |
FormatterBase:: |
public | function |
Returns a short summary for the current formatter settings. Overrides FormatterInterface:: |
22 |
FormatterBase:: |
public | function |
Builds a renderable array for a fully themed field. Overrides FormatterInterface:: |
1 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginSettingsBase:: |
protected | property | Whether default settings have been merged into the current $settings. | |
PluginSettingsBase:: |
protected | property | The plugin settings injected by third party modules. | |
PluginSettingsBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
6 |
PluginSettingsBase:: |
public static | function |
Defines the default settings for this plugin. Overrides PluginSettingsInterface:: |
42 |
PluginSettingsBase:: |
public | function |
Returns the value of a setting, or its default value if absent. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Returns the array of settings, including defaults for missing settings. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
protected | function | Merges default settings values into $settings. | |
PluginSettingsBase:: |
public | function |
Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsInterface:: |
3 |
PluginSettingsBase:: |
public | function |
Sets the value of a setting for the plugin. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Sets the settings for the plugin. Overrides PluginSettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
PluginSettingsBase:: |
public | function |
Unsets a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |