View source
<?php
namespace Drupal\dc_ajax_add_cart\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\commerce_cart\Form\AddToCartForm;
use Drupal\commerce_cart\Form\AddToCartFormInterface;
use Drupal\Component\Utility\Html;
use Drupal\dc_ajax_add_cart\RefreshPageElementsHelper;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\commerce_cart\CartManagerInterface;
use Drupal\commerce_cart\CartProviderInterface;
use Drupal\commerce_order\Resolver\OrderTypeResolverInterface;
use Drupal\commerce_store\CurrentStoreInterface;
use Drupal\commerce_price\Resolver\ChainPriceResolverInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AjaxAddToCartForm extends AddToCartForm implements AddToCartFormInterface {
protected $refreshPageElementsHelper;
public function __construct(EntityManagerInterface $entity_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time, CartManagerInterface $cart_manager, CartProviderInterface $cart_provider, OrderTypeResolverInterface $order_type_resolver, CurrentStoreInterface $current_store, ChainPriceResolverInterface $chain_price_resolver, AccountInterface $current_user, RefreshPageElementsHelper $refresh_page_elements_helper) {
parent::__construct($entity_manager, $entity_type_bundle_info, $time, $cart_manager, $cart_provider, $order_type_resolver, $current_store, $chain_price_resolver, $current_user);
$this->refreshPageElementsHelper = $refresh_page_elements_helper;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.manager'), $container
->get('entity_type.bundle.info'), $container
->get('datetime.time'), $container
->get('commerce_cart.cart_manager'), $container
->get('commerce_cart.cart_provider'), $container
->get('commerce_order.chain_order_type_resolver'), $container
->get('commerce_store.current_store'), $container
->get('commerce_price.chain_price_resolver'), $container
->get('current_user'), $container
->get('dc_ajax_add_cart.refresh_page_elements_helper'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form['#attached']['library'][] = 'core/jquery.form';
$form['#attached']['library'][] = 'core/drupal.ajax';
return $form;
}
protected function actions(array $form, FormStateInterface $form_state) {
$wrapper_id = Html::getUniqueId($this
->getFormId() . '-ajax-add-cart-wrapper');
$actions['submit'] = [
'#prefix' => '<div id="' . $wrapper_id . '">',
'#suffix' => '</div>',
'#type' => 'submit',
'#value' => $this
->t('Add to cart'),
'#submit' => [
'::submitForm',
],
'#attributes' => [
'class' => [
'use-ajax-submit',
],
],
'#ajax' => [
'callback' => '::refreshAddToCartForm',
'wrapper' => $wrapper_id,
],
];
return $actions;
}
public function refreshAddToCartForm(array $form, FormStateInterface $form_state) {
return $this->refreshPageElementsHelper
->updatePageElements($form)
->getResponse();
}
}