You are here

class OgGroupContextCacheContext in Organic groups 8

Defines a cache context service for the currently active group.

This uses OgContext to determine the active group. Potential use cases for this cache context are elements on the page that vary by the active group, for example a group header, or a block showing recent group content.

Cache context ID: 'og_group_context'

Hierarchy

Expanded class hierarchy of OgGroupContextCacheContext

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

File

src/Cache/Context/OgGroupContextCacheContext.php, line 21

Namespace

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

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

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

  /**
   * Constructs a new UserCacheContextBase class.
   *
   * @param \Drupal\og\OgContextInterface $og_context
   *   The OG context provider.
   */
  public function __construct(OgContextInterface $og_context) {
    $this->ogContext = $og_context;
  }

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

  /**
   * {@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;
    }

    // Compose a cache context string that consists of the entity type ID and
    // the entity ID of the active group.
    return implode(':', [
      $group
        ->getEntityTypeId(),
      $group
        ->id(),
    ]);
  }

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

}

Members

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