commerce_product_bundle-8.x-1.x-dev/src/Plugin/Field/FieldFormatter/AddToCartFormatter.php
src/Plugin/Field/FieldFormatter/AddToCartFormatter.php
<?php
namespace Drupal\commerce_product_bundle\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_product_bundle_add_to_cart' formatter.
*
* @FieldFormatter(
* id = "commerce_product_bundle_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 bundle items.'),
'#description' => t('The order item type, bundle item selections, 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 bundle items.');
}
else {
$summary[] = $this->t('Do not combine order items containing the same product bundle items.');
}
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
return [
'#lazy_builder' => [
'commerce_product_bundle.lazy_builders:addToCartForm', [
$items->getEntity()->id(),
$this->viewMode,
$this->getSetting('combine'),
],
],
'#create_placeholder' => TRUE,
];
}
/**
* {@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_bundle' && $field_name == 'bundle_items';
}
}
