You are here

class WishlistCacheContext in Commerce Wishlist 8.3

Defines the WishlistCacheContext service, for "per wishlist" caching.

Cache context ID: 'wishlist'.

Hierarchy

Expanded class hierarchy of WishlistCacheContext

1 file declares its use of WishlistCacheContext
WishlistCacheContextTest.php in tests/src/Unit/WishlistCacheContextTest.php
1 string reference to 'WishlistCacheContext'
commerce_wishlist.services.yml in ./commerce_wishlist.services.yml
commerce_wishlist.services.yml
1 service uses WishlistCacheContext
cache_context.wishlist in ./commerce_wishlist.services.yml
Drupal\commerce_wishlist\Cache\Context\WishlistCacheContext

File

src/Cache/Context/WishlistCacheContext.php, line 15

Namespace

Drupal\commerce_wishlist\Cache\Context
View source
class WishlistCacheContext implements CacheContextInterface {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $account;

  /**
   * The wishlist provider service.
   *
   * @var \Drupal\commerce_wishlist\WishlistProviderInterface
   */
  protected $wishlistProvider;

  /**
   * Constructs a new WishlistCacheContext object.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The current user account.
   * @param \Drupal\commerce_wishlist\WishlistProviderInterface $wishlist_provider
   *   The wishlist provider service.
   */
  public function __construct(AccountInterface $account, WishlistProviderInterface $wishlist_provider) {
    $this->account = $account;
    $this->wishlistProvider = $wishlist_provider;
  }

  /**
   * {@inheritdoc}
   */
  public static function getLabel() {
    return t('Current wishlist IDs');
  }

  /**
   * {@inheritdoc}
   */
  public function getContext() {
    return implode(':', $this->wishlistProvider
      ->getWishlistIds($this->account));
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheableMetadata() {
    $metadata = new CacheableMetadata();
    foreach ($this->wishlistProvider
      ->getWishlists($this->account) as $wishlist) {
      $metadata
        ->addCacheableDependency($wishlist);
    }
    return $metadata;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
WishlistCacheContext::$account protected property The current user.
WishlistCacheContext::$wishlistProvider protected property The wishlist provider service.
WishlistCacheContext::getCacheableMetadata public function Gets the cacheability metadata for the context. Overrides CacheContextInterface::getCacheableMetadata
WishlistCacheContext::getContext public function Returns the string representation of the cache context. Overrides CacheContextInterface::getContext
WishlistCacheContext::getLabel public static function Returns the label of the cache context. Overrides CacheContextInterface::getLabel
WishlistCacheContext::__construct public function Constructs a new WishlistCacheContext object.