class RouteGroupCacheContext in Group 2.0.x
Same name and namespace in other branches
- 8 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
- class \Drupal\group\Cache\Context\RouteGroupCacheContext implements CacheContextInterface uses GroupRouteContextTrait
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'
1 service uses RouteGroupCacheContext
File
- src/
Cache/ Context/ RouteGroupCacheContext.php, line 21
Namespace
Drupal\group\Cache\ContextView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
GroupRouteContextTrait:: |
protected | property | The current route match object. | |
GroupRouteContextTrait:: |
protected | property | The entity type manager service. | |
GroupRouteContextTrait:: |
protected | function | Gets the current route match object. | |
GroupRouteContextTrait:: |
protected | function | Gets the entity type manager service. | |
GroupRouteContextTrait:: |
public | function | Retrieves the group entity from the current route. | |
RouteGroupCacheContext:: |
public | function |
Gets the cacheability metadata for the context. Overrides CacheContextInterface:: |
|
RouteGroupCacheContext:: |
public | function |
Returns the string representation of the cache context. Overrides CacheContextInterface:: |
|
RouteGroupCacheContext:: |
public static | function |
Returns the label of the cache context. Overrides CacheContextInterface:: |
|
RouteGroupCacheContext:: |
public | function | Constructs a new RouteGroupCacheContext. |