commerce-8.x-2.8/modules/product/src/Plugin/Field/FieldFormatter/AddToCartFormatter.php
modules/product/src/Plugin/Field/FieldFormatter/AddToCartFormatter.php
<?php
namespace Drupal\commerce_product\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'commerce_add_to_cart' formatter.
*
* @FieldFormatter(
* id = "commerce_add_to_cart",
* label = @Translation("Add to cart form"),
* field_types = {
* "entity_reference",
* },
* )
*/
class AddToCartFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'combine' => TRUE,
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form['combine'] = [
'#type' => 'checkbox',
'#title' => t('Combine order items containing the same product variation.'),
'#description' => t('The order item type, referenced product variation, and data from fields exposed on the Add to Cart form must all match to combine.'),
'#default_value' => $this->getSetting('combine'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
if ($this->getSetting('combine')) {
$summary[] = $this->t('Combine order items containing the same product variation.');
}
else {
$summary[] = $this->t('Do not combine order items containing the same product variation.');
}
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$elements[0]['add_to_cart_form'] = [
'#lazy_builder' => [
'commerce_product.lazy_builders:addToCartForm', [
$items->getEntity()->id(),
$this->viewMode,
$this->getSetting('combine'),
$langcode,
],
],
'#create_placeholder' => TRUE,
];
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';
}
}
