You are here

class OgMembershipStateCacheContext in Organic groups 8

Defines a cache context service, for "membership state" caching.

Cache context ID: 'og_membership_state'

Hierarchy

Expanded class hierarchy of OgMembershipStateCacheContext

1 file declares its use of OgMembershipStateCacheContext
OgMembershipStateCacheContextTest.php in tests/src/Unit/Cache/Context/OgMembershipStateCacheContextTest.php
1 string reference to 'OgMembershipStateCacheContext'
og.services.yml in ./og.services.yml
og.services.yml
1 service uses OgMembershipStateCacheContext
cache_context.og_membership_state in ./og.services.yml
Drupal\og\Cache\Context\OgMembershipStateCacheContext

File

src/Cache/Context/OgMembershipStateCacheContext.php, line 20

Namespace

Drupal\og\Cache\Context
View source
class OgMembershipStateCacheContext implements CacheContextInterface {

  /**
   * The string to return when no context is found.
   */
  const NO_CONTEXT = 'none';

  /**
   * The membership manager service.
   *
   * @var \Drupal\og\MembershipManagerInterface
   */
  protected $membershipManager;

  /**
   * The OG context provider.
   *
   * @var \Drupal\og\OgContextInterface
   */
  protected $ogContext;

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

  /**
   * Constructs a new UserCacheContextBase class.
   *
   * @param \Drupal\Core\Session\AccountInterface $user
   *   The current user.
   * @param \Drupal\og\OgContextInterface $og_context
   *   The OG context provider.
   * @param \Drupal\og\MembershipManagerInterface $membership_manager
   *   The membership manager service.
   */
  public function __construct(AccountInterface $user, OgContextInterface $og_context, MembershipManagerInterface $membership_manager) {
    $this->user = $user;
    $this->ogContext = $og_context;
    $this->membershipManager = $membership_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function getLabel() {
    return new TranslatableMarkup('OG membership state');
  }

  /**
   * {@inheritdoc}
   */
  public function getContext() {

    // Do not provide a cache context if there is no group in the current
    // context.
    $group = $this->ogContext
      ->getGroup();
    if (empty($group)) {
      return self::NO_CONTEXT;
    }

    /** @var \Drupal\og\OgMembershipInterface $membership */
    $membership = $this->membershipManager
      ->getMembership($group, $this->user
      ->id(), OgMembershipInterface::ALL_STATES);
    return $membership ? $membership
      ->getState() : self::NO_CONTEXT;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheableMetadata() {
    return new CacheableMetadata();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OgMembershipStateCacheContext::$membershipManager protected property The membership manager service.
OgMembershipStateCacheContext::$ogContext protected property The OG context provider.
OgMembershipStateCacheContext::$user protected property The current user.
OgMembershipStateCacheContext::getCacheableMetadata public function Gets the cacheability metadata for the context. Overrides CacheContextInterface::getCacheableMetadata
OgMembershipStateCacheContext::getContext public function Returns the string representation of the cache context. Overrides CacheContextInterface::getContext
OgMembershipStateCacheContext::getLabel public static function Returns the label of the cache context. Overrides CacheContextInterface::getLabel
OgMembershipStateCacheContext::NO_CONTEXT constant The string to return when no context is found.
OgMembershipStateCacheContext::__construct public function Constructs a new UserCacheContextBase class.