AjaxCartHelper.php in Ajax Add to Cart 8
File
src/Helper/AjaxCartHelper.php
View source
<?php
namespace Drupal\ajax_add_to_cart\Helper;
use Drupal;
use Drupal\ajax_add_to_cart\Ajax\ReloadCommand;
use Drupal\block\Entity\Block;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Ajax\ReplaceCommand;
class AjaxCartHelper {
public static $helper = NULL;
protected $cartBlock;
protected $container;
protected $configFactory;
protected $messenger;
private function __construct() {
$this->container = Drupal::getContainer();
$this->cartBlock = $this
->getCartBlock($this->container);
$this->configFactory = $this->container
->get('config.factory');
$this->messenger = $this->container
->get('messenger');
}
private function getCartBlock($container = NULL) {
$blockId = $this
->getCartBlockId();
if ($blockId != FALSE) {
$block = Block::load($blockId);
$render = $container
->get('entity_type.manager')
->getViewBuilder('block')
->view($block);
}
return isset($render) ? $render : NULL;
}
private function getCartBlockId() {
$blockRepo = Drupal::service('block.repository');
$regions = $blockRepo
->getVisibleBlocksPerRegion();
foreach ($regions as $region) {
foreach ($region as $block) {
$idPlugin = $block
->get('plugin');
if ($idPlugin == 'commerce_cart') {
$cartBlockId = $block
->get('id');
return $cartBlockId;
}
}
}
return FALSE;
}
public static function getInstance() {
if (!self::$helper) {
self::$helper = new AjaxCartHelper();
}
return self::$helper;
}
public function ajaxAddToCartAjaxForm($form_id, &$form) {
$messages = [
$form_id => t('Adding to cart ...'),
];
$form['status_messages_' . $form_id] = [
'#type' => 'status_messages',
'#weight' => -10,
];
$form['form_id'] = [
'#type' => 'hidden',
'#value' => $form_id,
];
$form['actions']['submit']['#attributes']['class'][] = 'use-ajax';
$form['actions']['submit']['#ajax'] = [
'callback' => 'ajax_add_to_cart_ajax_validate',
'disable-refocus' => TRUE,
'event' => 'click',
'progress' => [
'type' => 'throbber',
'message' => $messages[$form_id],
],
];
$form['#attached']['library'][] = 'ajax_add_to_cart/ajax_add_to_cart.commands';
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$form['#attached']['drupalSettings']['ajax_add_to_cart']['ajax_add_to_cart']['time'] = $this->configFactory
->get('ajax_add_to_cart.ajaxconfig')
->get('time_ajax_modal');
return $form;
}
public function ajaxAddToCartAjaxResponse($form_id, $response) {
$options = [
'width' => $this->configFactory
->get('ajax_add_to_cart.ajaxconfig')
->get('ajax_modal_width'),
'height' => $this->configFactory
->get('ajax_add_to_cart.ajaxconfig')
->get('ajax_modal_height'),
];
$title = t('Successfully Added');
$message = [
'#theme' => 'status_messages',
'#message_list' => [
'status' => $this->messenger
->deleteByType('status'),
],
];
if (!empty($this->cartBlock)) {
$response
->addCommand(new OpenModalDialogCommand($title, $message, $options));
}
else {
$customblock = $this->container
->get('plugin.manager.block')
->createInstance('commerce_cart', []);
$render = $customblock
->build();
$response
->addCommand(new OpenModalDialogCommand($title, $render, $options));
}
$response
->addCommand(new ReplaceCommand('.block-commerce-cart', $this->cartBlock));
$response
->addCommand(new ReloadCommand());
return $response;
}
}