This repository has been archived by the owner on Aug 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 133
Og membership state cache context #163
Merged
amitaibu
merged 20 commits into
Gizra:8.x-1.x
from
amitaibu:OgMembershipStatusesCacheContext
Sep 20, 2016
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
4085fe4
Start adding cache context
amitaibu 06a4d6c
Add code
amitaibu 1103670
Sniffer fixes
amitaibu ae4d354
Add context
amitaibu e55ee7f
Fix label
amitaibu b66b66f
Start adding unit tests.
amitaibu 3eb100a
Add testNoGroupEntities
amitaibu 0444893
Add testNoGroupAndRouteParametersIntersection
amitaibu 45ebfe6
Sniffer fixes
amitaibu 4f4eb9e
Add testNoMembership
amitaibu ad169ad
Add testMembership
amitaibu 9b8a561
Merge branch '8.x-1.x' into OgMembershipStatusesCacheContext
amitaibu 0643338
Use constant when there is no context
amitaibu c7346d6
Convert another string to constant
amitaibu 7d52b1d
Convert text in string
amitaibu 1efa645
Sniffer fixes
amitaibu 8a5163a
Sniffer fixes
amitaibu 4d63124
Automatic sniffer fixes
amitaibu a52457a
Sniffer fixes
amitaibu 29f436d
Sniffer fixes
amitaibu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
|
||
namespace Drupal\og\Cache\Context; | ||
|
||
use Drupal\Core\Cache\CacheableMetadata; | ||
use Drupal\Core\Cache\Context\CacheContextInterface; | ||
use Drupal\Core\Routing\RouteMatchInterface; | ||
use Drupal\Core\Session\AccountInterface; | ||
use Drupal\og\GroupTypeManager; | ||
use Drupal\og\MembershipManagerInterface; | ||
use Drupal\og\OgMembershipInterface; | ||
|
||
/** | ||
* Defines a cache context service, for "membership state" caching. | ||
* | ||
* Cache context ID: 'og_membership_state' | ||
*/ | ||
class OgMembershipStateCacheContext implements CacheContextInterface { | ||
|
||
/** | ||
* The string to return when no context is found. | ||
*/ | ||
const NO_CONTEXT = 'none'; | ||
|
||
|
||
/** | ||
* The group type manager service. | ||
* | ||
* @var \Drupal\og\GroupTypeManager | ||
*/ | ||
protected $groupTypeManager; | ||
|
||
|
||
/** | ||
* The membership manager service. | ||
* | ||
* @var \Drupal\og\MembershipManagerInterface | ||
*/ | ||
protected $membershipManager; | ||
|
||
/** | ||
* The route match service. | ||
* | ||
* @var \Drupal\Core\Routing\RouteMatchInterface | ||
*/ | ||
protected $routeMatch; | ||
|
||
/** | ||
* 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\Core\Routing\RouteMatchInterface $route_match | ||
* The current route match service. | ||
* @param \Drupal\og\GroupTypeManager $group_type_manager | ||
* The group type manager service. | ||
* @param \Drupal\og\MembershipManagerInterface $membership_manager | ||
* The membership manager service. | ||
*/ | ||
public function __construct(AccountInterface $user, RouteMatchInterface $route_match, GroupTypeManager $group_type_manager, MembershipManagerInterface $membership_manager) { | ||
$this->user = $user; | ||
$this->routeMatch = $route_match; | ||
$this->groupTypeManager = $group_type_manager; | ||
$this->membershipManager = $membership_manager; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getLabel() { | ||
return t('OG membership state'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getContext() { | ||
if (!$route_contexts = $this->routeMatch->getRouteObject()->getOption('parameters')) { | ||
// No "parameters" defined in the route. | ||
return self::NO_CONTEXT; | ||
} | ||
|
||
if (!$entity_type_ids = array_keys($this->groupTypeManager->getAllGroupBundles())) { | ||
// No group entities. | ||
return self::NO_CONTEXT; | ||
} | ||
|
||
if (!$entity_type_ids = array_intersect(array_keys($route_contexts), $entity_type_ids)) { | ||
// No parameters that match the group entities. | ||
return self::NO_CONTEXT; | ||
} | ||
|
||
// Take just the first entity type ID. | ||
$entity_type_id = reset($entity_type_ids); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't work if there are groups using multiple entity types. In our project for example we have groups of type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It takes the first entity type ID that is a group (see the |
||
|
||
$group = $this->routeMatch->getParameter($entity_type_id); | ||
$states = [ | ||
OgMembershipInterface::STATE_ACTIVE, | ||
OgMembershipInterface::STATE_PENDING, | ||
OgMembershipInterface::STATE_BLOCKED, | ||
]; | ||
|
||
/** @var OgMembershipInterface $membership */ | ||
$membership = $this->membershipManager->getMembership($group, $this->user, $states); | ||
return $membership ? $membership->getState() : self::NO_CONTEXT; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getCacheableMetadata() { | ||
return new CacheableMetadata(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Depending directly on the route parameters severely limits the usefulness of this cache context. It will for example not be possible to use this on group content pages, because they won't match the route of the group to which they belong.
An example: in our project all group content has a header that displays the group to which the content belongs. In this header we have some local actions the user can take (for example join the group if they are not a member yet). So we need to vary by membership state cache context on our group content pages.
I am wondering if we can add the current group to the generic route context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is possible to add a 'runtimeContext' to the route (ref
ContextProviderInterface::getRuntimeContexts()
) - here we can dynamically set the right group that matches the current route. It should be possible to do this plugin based like was already being worked on.We should use this runtimeContext to determine the cache context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the pointer on
:: getRuntimeContexts
, I'll check it outMy recent thought about OG context is, for the sake of simplicity not port along with Plugins, but rather have a single ContextProvider.
So maybe, In your case, instead of the plugins you could extend the cache context and add your logic. How does it sound?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well now it only works on a very specific subset of routes that follow very specific rules (like being formatted as
node/%
). By leveraging the OG context it would work out of the box on all routes that have a group associated with them. That sounds more useful to me. It would of course also work onnode/%
).I will have to look into this though, I start to understand caching a bit better lately but I am still unsure about some aspects of it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, OG context might be needed after all. /cc @RoySegall
On Sep 7, 2016 3:25 PM, "Pieter Frenssen" notifications@github.com wrote:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I think the case is simpler. The cache context is added only in the formater of a field attached directly to a group (subscribe to group). So getting the group in this case is tightly coupled with the route, thus og context would not be needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Although in general, and not related to this pr, og context might still be needed - not sure)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pfrenssen ^^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This field-specific cache context would work fine for this use case, but it would be obsolete when we get the generic cache context that uses OgContext, since that will also cover its use case. So this implementation is temporary at best.
In our project we now need the generic cache context for a group header that shows some information about the group. This information is different depending on the membership status so we need this cache context.
I can work on this now. I'll have a look at @RoySegall 's ticket, maybe I can help there to move things forward.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, I think we can have it as a follow up, once we have
OgContextProvider
in place