class AddToWishlistController in Commerce Add To Cart Link 8
Same name and namespace in other branches
- 2.x modules/wishlist/src/Controller/AddToWishlistController.php \Drupal\commerce_add_to_wishlist_link\Controller\AddToWishlistController
Defines the add to wishlist controller.
The controller enables product variations to be added via GET requests.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\commerce_add_to_wishlist_link\Controller\AddToWishlistController
Expanded class hierarchy of AddToWishlistController
File
- modules/
wishlist/ src/ Controller/ AddToWishlistController.php, line 20
Namespace
Drupal\commerce_add_to_wishlist_link\ControllerView source
class AddToWishlistController extends ControllerBase {
/**
* The cart link token service.
*
* @var \Drupal\commerce_add_to_cart_link\CartLinkTokenInterface
*/
protected $cartLinkToken;
/**
* The wishlist manager.
*
* @var \Drupal\commerce_wishlist\WishlistManagerInterface
*/
protected $wishlistManager;
/**
* The wishlist provider.
*
* @var \Drupal\commerce_wishlist\WishlistProviderInterface
*/
protected $wishlistProvider;
/**
* Constructs a new AddToWishlistController object.
*
* @param \Drupal\commerce_add_to_cart_link\CartLinkTokenInterface $cart_link_token
* The cart link token service.
* @param \Drupal\commerce_wishlist\WishlistManagerInterface $wishlist_manager
* The wishlist manager.
* @param \Drupal\commerce_wishlist\WishlistProviderInterface $wishlist_provider
* The wishlist provider.
*/
public function __construct(CartLinkTokenInterface $cart_link_token, WishlistManagerInterface $wishlist_manager, WishlistProviderInterface $wishlist_provider) {
$this->cartLinkToken = $cart_link_token;
$this->wishlistManager = $wishlist_manager;
$this->wishlistProvider = $wishlist_provider;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* Performs the add to wishlist action and redirects to wishlist.
*
* @param \Drupal\commerce_product\Entity\ProductInterface $commerce_product
* The product entity.
* @param \Drupal\commerce_product\Entity\ProductVariationInterface $commerce_product_variation
* The product variation to add.
* @param string $token
* The CSRF token.
* @param \Symfony\Component\HttpFoundation\Request $request
* The request.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* A redirect to the wishlist after adding the product.
*
* @throws \Exception
* When the call to self::selectStore() throws an exception because the
* entity can't be purchased from the current store.
*/
public function action(ProductInterface $commerce_product, ProductVariationInterface $commerce_product_variation, $token, Request $request) {
$quantity = $request->query
->get('quantity', 1);
// Determine the wishlist type to use.
$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);
return $this
->redirect('commerce_wishlist.page');
}
/**
* Access callback for the action route.
*
* @param \Drupal\commerce_product\Entity\ProductInterface $commerce_product
* The product entity.
* @param \Drupal\commerce_product\Entity\ProductVariationInterface $commerce_product_variation
* The product variation to add.
* @param string $token
* The CSRF token.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
public function access(ProductInterface $commerce_product, ProductVariationInterface $commerce_product_variation, $token) {
if (!$commerce_product
->isPublished() || !$commerce_product
->access('view')) {
// If product is disabled or the user has no view access, deny.
return AccessResult::forbidden();
}
if (!$commerce_product_variation
->isPublished() || !$commerce_product_variation
->access('view')) {
// If the variation is inactive, deny.
return AccessResult::forbidden();
}
if ((int) $commerce_product
->id() !== (int) $commerce_product_variation
->getProductId()) {
// Deny, if the product ID and variation's parent product ID don't match.
return AccessResult::forbidden();
}
return AccessResult::allowedIf($this->cartLinkToken
->validate($commerce_product_variation, $token));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AddToWishlistController:: |
protected | property | The cart link token service. | |
AddToWishlistController:: |
protected | property | The wishlist manager. | |
AddToWishlistController:: |
protected | property | The wishlist provider. | |
AddToWishlistController:: |
public | function | Access callback for the action route. | |
AddToWishlistController:: |
public | function | Performs the add to wishlist action and redirects to wishlist. | |
AddToWishlistController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
AddToWishlistController:: |
public | function | Constructs a new AddToWishlistController object. | |
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |