AddToWishlistController.php in Commerce Add To Cart Link 2.x
File
modules/wishlist/src/Controller/AddToWishlistController.php
View source
<?php
namespace Drupal\commerce_add_to_wishlist_link\Controller;
use Drupal\commerce_add_to_cart_link\CartLinkTokenInterface;
use Drupal\commerce_product\Entity\ProductInterface;
use Drupal\commerce_product\Entity\ProductVariationInterface;
use Drupal\commerce_wishlist\WishlistManagerInterface;
use Drupal\commerce_wishlist\WishlistProviderInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Path\PathValidatorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
class AddToWishlistController extends ControllerBase {
protected $cartLinkToken;
protected $wishlistManager;
protected $wishlistProvider;
protected $pathValidator;
public function __construct(CartLinkTokenInterface $cart_link_token, WishlistManagerInterface $wishlist_manager, WishlistProviderInterface $wishlist_provider, PathValidatorInterface $path_validator) {
$this->cartLinkToken = $cart_link_token;
$this->wishlistManager = $wishlist_manager;
$this->wishlistProvider = $wishlist_provider;
$this->pathValidator = $path_validator;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('commerce_add_to_cart_link.token'), $container
->get('commerce_wishlist.wishlist_manager'), $container
->get('commerce_wishlist.wishlist_provider'), $container
->get('path.validator'));
}
public function action(ProductInterface $commerce_product, ProductVariationInterface $commerce_product_variation, $token, Request $request) {
$quantity = $request->query
->get('quantity', 1);
$wishlist_type = $this
->config('commerce_wishlist.settings')
->get('default_type') ?: 'default';
$wishlist = $this->wishlistProvider
->getWishlist($wishlist_type);
if (!$wishlist) {
$wishlist = $this->wishlistProvider
->createWishlist($wishlist_type);
}
$this->wishlistManager
->addEntity($wishlist, $commerce_product_variation, $quantity);
if ($this
->config('commerce_add_to_cart_link.settings')
->get('redirect_back')) {
$referer = $request->server
->get('HTTP_REFERER');
$fake_request = Request::create($referer);
$referer_url = $this->pathValidator
->getUrlIfValid($fake_request
->getRequestUri());
if ($referer_url && $referer_url
->isRouted() && $referer_url
->getRouteName() !== 'user.login') {
$referer_url
->setAbsolute();
return new RedirectResponse($referer_url
->toString());
}
}
return $this
->redirect('commerce_wishlist.page');
}
public function access(ProductInterface $commerce_product, ProductVariationInterface $commerce_product_variation, $token) {
if (!$commerce_product
->isPublished() || !$commerce_product
->access('view')) {
return AccessResult::forbidden();
}
if (!$commerce_product_variation
->isPublished() || !$commerce_product_variation
->access('view')) {
return AccessResult::forbidden();
}
if ((int) $commerce_product
->id() !== (int) $commerce_product_variation
->getProductId()) {
return AccessResult::forbidden();
}
return AccessResult::allowedIf($this->cartLinkToken
->validate($commerce_product_variation, $token));
}
}