You are here

class AddToWishlistLink in Commerce Add To Cart Link 2.x

Same name and namespace in other branches
  1. 8 modules/wishlist/src/AddToWishlistLink.php \Drupal\commerce_add_to_wishlist_link\AddToWishlistLink

Defines a helper class for constructing add to wishlist links.

Hierarchy

Expanded class hierarchy of AddToWishlistLink

1 file declares its use of AddToWishlistLink
commerce_add_to_wishlist_link.module in modules/wishlist/commerce_add_to_wishlist_link.module
Hook implementations of commerce_add_to_wishlist_link module.

File

modules/wishlist/src/AddToWishlistLink.php, line 12

Namespace

Drupal\commerce_add_to_wishlist_link
View source
class AddToWishlistLink {

  /**
   * The product variation.
   *
   * @var \Drupal\commerce_product\Entity\ProductVariationInterface
   */
  protected $variation;

  /**
   * Constructs a new AddToWishlistLink object.
   *
   * @param \Drupal\commerce_product\Entity\ProductVariationInterface $variation
   *   The product variation.
   */
  public function __construct(ProductVariationInterface $variation) {
    $this->variation = $variation;
  }

  /**
   * Generate a render array for an add-to-wishlist link.
   *
   * @return array
   *   The render array.
   */
  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;
  }

  /**
   * Generate an URL object for an add-to-wishlist 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_wishlist_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() {
    return BubbleableMetadata::createFromRenderArray([
      '#cache' => [
        'contexts' => [
          'user.roles',
        ],
        'tags' => [
          'config:commerce_add_to_cart_link.settings',
        ],
      ],
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AddToWishlistLink::$variation protected property The product variation.
AddToWishlistLink::build public function Generate a render array for an add-to-wishlist link.
AddToWishlistLink::metadata public function Generate metadata for an add-to-cart link.
AddToWishlistLink::url public function Generate an URL object for an add-to-wishlist link.
AddToWishlistLink::__construct public function Constructs a new AddToWishlistLink object.