AddToWishlistLink.php in Commerce Add To Cart Link 8
File
modules/wishlist/src/AddToWishlistLink.php
View source
<?php
namespace Drupal\commerce_add_to_wishlist_link;
use Drupal\commerce_product\Entity\ProductVariationInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Url;
class AddToWishlistLink {
protected $variation;
public function __construct(ProductVariationInterface $variation) {
$this->variation = $variation;
}
public function build() {
$build = [
'#theme' => 'commerce_add_to_wishlist_link',
'#url' => $this
->url(),
'#product_variation' => $this->variation,
];
$metadata = $this
->metadata();
$metadata
->applyTo($build);
return $build;
}
public function url() {
$cart_link_token_service = \Drupal::service('commerce_add_to_cart_link.token');
return Url::fromRoute('commerce_add_to_wishlist_link.page', [
'commerce_product' => $this->variation
->getProductId(),
'commerce_product_variation' => $this->variation
->id(),
'token' => $cart_link_token_service
->generate($this->variation),
]);
}
public function metadata() {
$cart_link_token_service = \Drupal::service('commerce_add_to_cart_link.token');
$contexts = $cart_link_token_service
->needsCsrfProtection() ? [
'session',
] : [];
return BubbleableMetadata::createFromRenderArray([
'#cache' => [
'contexts' => $contexts,
'tags' => [
'config:commerce_add_to_cart_link.settings',
],
],
]);
}
}