View source
<?php
namespace Drupal\commerce_cart\Plugin\views\field;
use Drupal\commerce_cart\CartManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\Plugin\views\field\UncacheableFieldHandlerTrait;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EditQuantity extends FieldPluginBase {
use UncacheableFieldHandlerTrait;
protected $cartManager;
protected $entityTypeManager;
protected $messenger;
public function __construct(array $configuration, $plugin_id, $plugin_definition, CartManagerInterface $cart_manager, EntityTypeManagerInterface $entity_type_manager, MessengerInterface $messenger) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->cartManager = $cart_manager;
$this->entityTypeManager = $entity_type_manager;
$this->messenger = $messenger;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('commerce_cart.cart_manager'), $container
->get('entity_type.manager'), $container
->get('messenger'));
}
public function clickSortable() {
return FALSE;
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['allow_decimal'] = [
'default' => FALSE,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['allow_decimal'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Allow decimal quantities'),
'#default_value' => $this->options['allow_decimal'],
];
}
public function getValue(ResultRow $row, $field = NULL) {
return '<!--form-item-' . $this->options['id'] . '--' . $row->index . '-->';
}
public function viewsForm(array &$form, FormStateInterface $form_state) {
$form['#cache']['max-age'] = 0;
if (empty($this->view->result)) {
unset($form['actions']);
return;
}
$form['#attached'] = [
'library' => [
'commerce_cart/cart_form',
],
];
$form[$this->options['id']]['#tree'] = TRUE;
foreach ($this->view->result as $row_index => $row) {
$order_item = $this
->getEntity($row);
if ($this->options['allow_decimal']) {
$form_display = commerce_get_entity_display('commerce_order_item', $order_item
->bundle(), 'form');
$quantity_component = $form_display
->getComponent('quantity');
$step = $quantity_component['settings']['step'];
$precision = $step >= '1' ? 0 : strlen($step) - 2;
}
else {
$step = 1;
$precision = 0;
}
if (!$order_item
->isLocked()) {
$form[$this->options['id']][$row_index] = [
'#type' => 'number',
'#title' => $this
->t('Quantity'),
'#title_display' => 'invisible',
'#default_value' => round($order_item
->getQuantity(), $precision),
'#size' => 4,
'#min' => 0,
'#max' => 9999,
'#step' => $step,
'#required' => TRUE,
'#attributes' => [
'class' => [
'quantity-edit-input',
],
],
];
}
else {
$form[$this->options['id']][$row_index] = [
'#type' => 'item',
'#plain_text' => round($order_item
->getQuantity(), $precision),
];
}
}
$form['actions']['submit']['#update_cart'] = TRUE;
$form['actions']['submit']['#show_update_message'] = TRUE;
$form['actions']['submit']['#value'] = $this
->t('Update cart');
}
public function viewsFormValidate(array &$form, FormStateInterface $form_state) {
$triggering_element = $form_state
->getTriggeringElement();
if (empty($triggering_element['#update_cart'])) {
return;
}
$quantities = $form_state
->getValue($this->options['id'], []);
foreach ($quantities as $row_index => $quantity) {
if (!is_numeric($quantity) || $quantity <= 0) {
continue;
}
$order_item = $this
->getEntity($this->view->result[$row_index]);
if ($order_item
->getQuantity() == $quantity) {
continue;
}
$original_quantity = $order_item
->getQuantity();
$order_item
->setQuantity($quantity);
$violations = $order_item
->validate()
->filterByFields(array_diff(array_keys($order_item
->getFieldDefinitions()), [
'purchased_entity',
'quantity',
]));
foreach ($violations as $violation) {
$form_state
->setError($form[$this->options['id']][$row_index], $violation
->getMessage());
}
$order_item
->setQuantity($original_quantity);
}
}
public function viewsFormSubmit(array &$form, FormStateInterface $form_state) {
$triggering_element = $form_state
->getTriggeringElement();
if (empty($triggering_element['#update_cart'])) {
return;
}
$order_storage = $this->entityTypeManager
->getStorage('commerce_order');
$cart = $order_storage
->load($this->view->argument['order_id']
->getValue());
$quantities = $form_state
->getValue($this->options['id'], []);
$save_cart = FALSE;
foreach ($quantities as $row_index => $quantity) {
if (!is_numeric($quantity) || $quantity < 0) {
continue;
}
$order_item = $this
->getEntity($this->view->result[$row_index]);
if ($order_item
->getQuantity() == $quantity) {
continue;
}
if ($quantity > 0) {
$order_item
->setQuantity($quantity);
$this->cartManager
->updateOrderItem($cart, $order_item, FALSE);
}
else {
$this->cartManager
->removeOrderItem($cart, $order_item, FALSE);
}
$save_cart = TRUE;
}
if ($save_cart) {
$cart
->save();
if (!empty($triggering_element['#show_update_message'])) {
$this->messenger
->addMessage($this
->t('Your shopping cart has been updated.'));
}
}
}
public function query() {
}
}