class AddToCartLink in Commerce Add To Cart Link 8
Same name and namespace in other branches
- 2.x src/AddToCartLink.php \Drupal\commerce_add_to_cart_link\AddToCartLink
Defines a helper class for constructing add to cart links.
Hierarchy
- class \Drupal\commerce_add_to_cart_link\AddToCartLink
Expanded class hierarchy of AddToCartLink
2 files declare their use of AddToCartLink
- CommerceAddToCartLinkViewsField.php in src/
Plugin/ views/ field/ CommerceAddToCartLinkViewsField.php - commerce_add_to_cart_link.module in ./
commerce_add_to_cart_link.module - Hook implementations of commerce_add_to_cart_link module.
File
- src/
AddToCartLink.php, line 13
Namespace
Drupal\commerce_add_to_cart_linkView source
class AddToCartLink {
/**
* The product variation.
*
* @var \Drupal\commerce_product\Entity\ProductVariationInterface
*/
protected $variation;
/**
* Constructs a new AddToCartLink object.
*
* @param \Drupal\commerce_product\Entity\ProductVariationInterface $variation
* The product variation.
*/
public function __construct(ProductVariationInterface $variation) {
$this->variation = $variation;
}
/**
* AddToCartLink convenience constructor.
*
* @param int $id
* The product variation ID.
*
* @return \Drupal\commerce_add_to_cart_link\AddToCartLink
* The constructed object.
*/
public static function fromVariationId($id) {
if ($variation = ProductVariation::load($id)) {
return new static($variation);
}
else {
throw new \UnexpectedValueException('Can not load product variation: %s', $id);
}
}
/**
* Generate a render array for an add-to-cart link.
*
* @return array
* The render array.
*/
public function build() {
$build = [
'#theme' => 'commerce_add_to_cart_link',
'#url' => $this
->url(),
'#product_variation' => $this->variation,
];
$metadata = $this
->metadata();
$metadata
->applyTo($build);
return $build;
}
/**
* Generate an URL object for an add-to-cart link.
*
* @return \Drupal\Core\Url
* The URL object.
*/
public function url() {
/** @var \Drupal\commerce_add_to_cart_link\CartLinkTokenInterface $cart_link_token_service */
$cart_link_token_service = \Drupal::service('commerce_add_to_cart_link.token');
return Url::fromRoute('commerce_add_to_cart_link.page', [
'commerce_product' => $this->variation
->getProductId(),
'commerce_product_variation' => $this->variation
->id(),
'token' => $cart_link_token_service
->generate($this->variation),
]);
}
/**
* Generate metadata for an add-to-cart link.
*
* @return \Drupal\Core\Render\BubbleableMetadata
* The metadata object.
*/
public function metadata() {
/** @var \Drupal\commerce_add_to_cart_link\CartLinkTokenInterface $cart_link_token_service */
$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',
],
],
]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AddToCartLink:: |
protected | property | The product variation. | |
AddToCartLink:: |
public | function | Generate a render array for an add-to-cart link. | |
AddToCartLink:: |
public static | function | AddToCartLink convenience constructor. | |
AddToCartLink:: |
public | function | Generate metadata for an add-to-cart link. | |
AddToCartLink:: |
public | function | Generate an URL object for an add-to-cart link. | |
AddToCartLink:: |
public | function | Constructs a new AddToCartLink object. |