You are here

class RouteGroupCacheContext in Group 8

Same name and namespace in other branches
  1. 2.0.x src/Cache/Context/RouteGroupCacheContext.php \Drupal\group\Cache\Context\RouteGroupCacheContext

Defines a cache context for "per group from route" caching.

Please note: This cache context uses the group from the current route as the context value to work with. This context is therefore only to be used with data that was based on the group from the route. A good example being the 'entity:group' context provided by the 'group.group_route_context' service.

Cache context ID: 'route.group'.

Hierarchy

Expanded class hierarchy of RouteGroupCacheContext

1 file declares its use of RouteGroupCacheContext
RouteGroupCacheContextTest.php in tests/src/Unit/RouteGroupCacheContextTest.php
1 string reference to 'RouteGroupCacheContext'
group.services.yml in ./group.services.yml
group.services.yml
1 service uses RouteGroupCacheContext
cache_context.route.group in ./group.services.yml
Drupal\group\Cache\Context\RouteGroupCacheContext

File

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

Namespace

Drupal\group\Cache\Context
View source
class RouteGroupCacheContext implements CacheContextInterface {
  use GroupRouteContextTrait;

  /**
   * Constructs a new RouteGroupCacheContext.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $current_route_match
   *   The current route match object.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(RouteMatchInterface $current_route_match, EntityTypeManagerInterface $entity_type_manager) {
    $this->currentRouteMatch = $current_route_match;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function getLabel() {
    return t('Group from route');
  }

  /**
   * {@inheritdoc}
   */
  public function getContext() {
    if ($group = $this
      ->getGroupFromRoute()) {

      // If a group was found on the route, we return its ID as the context.
      if ($gid = $group
        ->id()) {
        return $gid;
      }

      // If there was no ID, but we still have a group, we are on the group add
      // form and we return the group type instead. This allows the context to
      // distinguish between unsaved groups, based on their type.
      return $group
        ->bundle();
    }

    // If no group was found on the route, we return a string that cannot be
    // mistaken for a group ID or group type.
    return 'group.none';
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
GroupRouteContextTrait::$currentRouteMatch protected property The current route match object.
GroupRouteContextTrait::$entityTypeManager protected property The entity type manager service.
GroupRouteContextTrait::getCurrentRouteMatch protected function Gets the current route match object.
GroupRouteContextTrait::getEntityTypeManager protected function Gets the entity type manager service.
GroupRouteContextTrait::getGroupFromRoute public function Retrieves the group entity from the current route.
RouteGroupCacheContext::getCacheableMetadata public function Gets the cacheability metadata for the context. Overrides CacheContextInterface::getCacheableMetadata
RouteGroupCacheContext::getContext public function Returns the string representation of the cache context. Overrides CacheContextInterface::getContext
RouteGroupCacheContext::getLabel public static function Returns the label of the cache context. Overrides CacheContextInterface::getLabel
RouteGroupCacheContext::__construct public function Constructs a new RouteGroupCacheContext.