Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
So far the group module provides one context service, GroupRouteContext, which takes the context from the path.
The views groupPermission handler ONLY takes its context from the path.
However I would like contexts of:
- all the groups the current user is in (perhaps filtered by group type)
- all the groups the current content is in.
Does it make sense for contexts to return several groups?
Comment | File | Size | Author |
---|---|---|---|
#52 | group-context-2815971-52.patch | 7.76 KB | banoodle |
#51 | group-context-2815971-51.patch | 7.74 KB | camslice |
#49 | group-context-2815971-49.patch | 7.76 KB | Ruslan Piskarov |
#48 | interdiff-46-47.txt | 647 bytes | billywardrop |
#48 | group-context-2815971-47.patch | 7.81 KB | billywardrop |
Comments
Comment #2
matslats CreditAttribution: matslats as a volunteer commentedI've created a new context, which returns all the groups of which the current user is a member.
I have modified the @ViewsAccess plugin group_permission so that you can choose to get the group context from the route or (the first group of) the current user.
Do you want this?
I would like to go further and specify in the @ViewsAccess plugin which grouptype in the current users's context we are interested in.
Do you want this?
Comment #3
matslats CreditAttribution: matslats as a volunteer commentedOk I've created a new GroupComboContext which returns an array of groups based on a cascade of possible contexts.
First of all is the route context, which would return only one group,
If the route doesn't indicate a group it falls back to a current user context, returning many groups
If the user isn't in any groups it falls back to the content being viewed, returning the groups it is in.
I also made a GroupComboSelection @EntityReferenceSelection plugin based on the GroupComboContext. It can be configured to select only groups that use certain plugins.
Shall I put all this in my own distro or shall I create a patch and post here?
Comment #4
rachel_norfolkAbsolutely yes - post it as a patch - it might be VERY useful!
I'm starting to think that it relates to my query in 2821513 where I'm trying to create a block view of other content in the same group. If I had a "group this content belongs to" context, that would be possible!
Comment #5
kristiaanvandeneyndeContexts (as used for blocks etc.) preferably have a predictable outcome. "Grab the group from the route" should give you the result you expect. A context that asks you for the user's first group may be confusing, what is the person's first group? There's some ambiguity there.
I would love to see more contexts, though! But they have to produce predictable output. I would also love to see more Views plugins for contextual filters. If you have any ideas that are generic enough, please post them as patches!
Comment #6
matslats CreditAttribution: matslats as a volunteer commentedI've made a module I'm calling group_exclusive.
It adds a boolean setting on the GroupContentEnabler when the entity cardinality is 1
When enabled the content MUST be added to one and only one group of that type.
I'm using this for users.
So the module then provides a context of the group of the current user.
I didn't mention it before because I supposed you wouldn't want this in the group module.
Comment #7
kristiaanvandeneyndeWell, we do have group cardinality for that purpose (only adding a node to a single group). But that is plugin-specific. So if another plugin were to allow you to add nodes, it could still lead to multiple parent groups per node.
You could try to make your module overrule the config for any node-serving plugin to enforce a group cardinality of 1.
Comment #8
matslats CreditAttribution: matslats as a volunteer commentedEnforcing a cardinality of 1 has UI and DX consequences.
For example the user/add form needs to know, by the time the user entity is saved, which group it is in.
So I made a new module to keep all that stuff separate.
Comment #9
ericras CreditAttribution: ericras commented@matslats I'd like to see GroupComboContext if you could post a patch. That sounds like what Organic Groups does with OgContext::getGroup(). I previously used the Drupal 7 version of that to get the all the contexts/groups of the node being viewed.
Comment #10
ericras CreditAttribution: ericras commentedAmbiguity is certainly an issue when something is assigned to multiple groups.
Here's an example: When viewing a node, I want to set the site title to the title/label of the group it is a part of. If it's just assigned to one group that is clean but if it's assigned to two or more groups then which is chosen?
OG's OgContext::getGroup() returns the "best candidate" based on its formula but its still just picking one. Its not very predictable because there's really no concept of the "main group" built into OG or Group. However, when there are multiple groups involved and you can only use one of them (e.g. to set the site title) then you just have to pick one somehow.
Possibly the solution is to name the proposed GroupComboContext something like GroupBestGuessContext. That way at least it indicates that it semi-arbitrarily returns one of many.
Edit: I just realized that this problem is similar to the ambiguity of having a delta-less [node:group] token in #2774827: Get a token of a node's parent group to create a pathauto pattern
Comment #11
matslats CreditAttribution: matslats as a volunteer commented@ericas sorry without any kind of go-ahead from @kristiaanvandeneynde I found another approach to my problem and I didn't have anywhere to retain that GroupCombo Context
Comment #12
mErilainen CreditAttribution: mErilainen at Wunder commentedI'm also interested on the topic of multiple groups per content. Should we create a canonical url per group? How can I display the link to the content in a list when the content has multiple groups, thus multiple contexts where it can be opened in. A clean url per group would look nice, but not sure how difficult it is to implement.
Another approach is to configure views to append a ?gid=x parameter to the content url depending on the currently active group context and then use some custom code to activate that group context when viewing content. Not sure if that's the right approach. Is it good for SEO when the content is the same but surrounding elements change depending on the parameter value.
Comment #13
ericras CreditAttribution: ericras commentedI'm going to throw up a patch that I'm using as a starting point. It does a simplified version of what OG does with getBestCandidate(). If you're viewing a node it grabs all the groups that it is a part of and it returns one of them to set the context.
I'm using this patch because I want the "Group ID from URL" default value in a Views Contextual Filter to behave like the OG one ("Current OG group from context").
Comment #14
tomasbarej CreditAttribution: tomasbarej commentedI was using #13 patch successfully but I've run into errors when we enabled revision of nodes.
{node} parameter of node revision route was returned as int instead of node object in getBestCandidate:
Comment #15
akalata CreditAttribution: akalata at Tag1 Consulting commentedHere's an update to #14 using the
currentRouteMatch
service that is already being injected into the class.Comment #16
SocialNicheGuru CreditAttribution: SocialNicheGuru commentedComment #17
adamfranco CreditAttribution: adamfranco at Middlebury College commentedI have the situation of needing to display a view of a users Groups where each of the Groups is rendered using a display mode that in turn uses an attached EVA view to list all of nodes associated with this group. Something like this:
I've set up the EVA view to use "GroupPermission" and this works fine when viewing the group directly, but found that it can't work when the group is in a view result because the Group Id is passed to the EVA view as an argument rather than being in the route context of the parent view. I don't want to unnecessarily hijack this issue, but I also don't want to pollute the queue with duplicates and think that this might be a very similar problem to those reported above -- a need to get the group from a different context than just the current route -- in this case a views argument.
In my particular use-case I can work around this GroupPermission failure by removing the access check in the EVA group since the groups listed are only those that the user is a member of and all members can view all nodes in the group. In other use-cases where group-roles have different view permissions checking the group permissions properly for each group would be necessary.
Comment #18
adamfranco CreditAttribution: adamfranco at Middlebury College commentedPlease ignore my comment #17 -- I think that may be a distraction from the direction of this thread.
I found that patch #15 will fail if a block displayed on a node-view url uses the Group from Route context. This exception is caused by the
GroupCacheContext
which is still looking for the group in the route rather than usinggetBestCandidate()
method likeGroupRouteContext
does in #15:Attached is a patch which builds on #15 to unify the operation of
GroupRouteContext
andGroupCacheContext
so that they both behave the same way.Comment #19
jastraat CreditAttribution: jastraat at Technivant commentedRerolled patch from #18 to work with latest dev.
Example of where this change would be key - the group_content_menu module: https://www.drupal.org/project/group_content_menu
This patch allows nodes within the group to still return the context of their group so that the group menu displays across all the group pages, not just on the group itself.
Is there any chance of getting something like this committed? If so, does the patch need to be enhanced so that there's an administrative setting to enable the extended functionality so that it doesn't break existing implementations? (E.g. sites where a single node is associated with multiple groups and choosing a single group as the group context makes less sense.)
Comment #21
anruetherI just installed #18 and it works nicely! (I'm still on RC5)
I have a title block that is displayed on group pages only. Formerly it only displayed on the group pages itself, that was its purpose. With the patch it is also displayed on all group nodes. For our use case (apart from the title block..) this would be the expected behaviour. But we only ever add a node to one group, in other cases I'm not sure how you find out which group's context shall be used.
Comment #22
anruetherWhen using #19 with group 1.3 I run into this error when visiting e.g.
/node/4/revisions/8/view
This does not happen when I limit a group views block to not appear on revision pages but only on
node/*
Comment #23
ChrisSnyderThe following patch builds upon patch #19 to account for node revision issue @anruether experienced and to use the first group in array if there are multiple instead of last.
Comment #24
heddnThis should use the 3rd argument to in_array, as pass TRUE.
This also assumes that all group content are nodes. While a good starting place, it would be nice if that wasn't the only option. How can we make this extensible? Is there any way we can find out if the thing in the URL is a group content item? An event subscriber is one way, but are there other better ways?
Comment #25
kristiaanvandeneyndeUsing the current route match's getParameters, you could grab said parameter and check if it's an instance of GroupContentInterface or call getEntityType() on it to find out that way.
Comment #26
heddnThis works across entity types, not just node. Probably could use some automated testing, but with manual testing, it works perfectly.
Comment #28
edysmpAdding some tests.
Comment #29
anruetherThanks for the work on this patch! After updating to core 9.1.5 I my local php server aborts in the following scenario:
Create a group views block (which creates a link to the group content overview page if users have permission)
Add an access check to the view, e.g.
Access: Group permission | Access group node overview
Add block to group content nodes (via block layout)
Now when visiting a node where the block is active, my server hangs
When either removing the patch or the group access check of the view I don't get the error. The log has nothing. I tested #19, #26 and #28. With core 8.9 the exact same setup was working.
Comment #30
scotwith1tUsing patch at #28 and works as advertised. Thanks!
Comment #31
Chi CreditAttribution: Chi commentedThe patch extends #28 to address this issue.
Comment #32
Chi CreditAttribution: Chi commentedMade the patch compatible with PHP 7.3.
Comment #33
Chi CreditAttribution: Chi commentedNote that the current "getBestCandidate" approach does not handle ambigutiy issue discussed in #5-#12. Even though it works well on many projects it is not suitable for everyone. Consequently, there is nothing to review yet.
I think the only generic solution possible here is to invoke some hook or event subscriber wich would allow to provide correct group context from a custom module.
Comment #34
markdc#32 working on PHP 7.4. Thanks
Comment #35
le72I also faced this problem. Need a parent group context on a node page. Using Layout Builder.
Patch didn't help :-( Maybe I am doing something wrong.
I have a custom block defined as
When placing the block in a section region I don't see the second (group) context.
More an easy step to reproduce the problem. Try to place "Group operations" block on a node page. Expectation: If the node belongs to a group, show that group's operations.
Any help?
Thank you in advance.
Comment #36
le72If you are using group v3.x the patch will need some changes. I replaced "group_content" with "group_relationship" and it works fine on Drupal 9.4.x with Group 3.x
Comment #37
le72Update patch for 3.0.0-betta5
Comment #38
Artyom Hovasapyan CreditAttribution: Artyom Hovasapyan commentedUpdate.
Comment #39
Artyom Hovasapyan CreditAttribution: Artyom Hovasapyan commentedWhen user group_member a lot of groups, #37 returned first element(if it user_page), I suggest patch which shows only for NodeInterface.
Update #37.
Comment #40
Artyom Hovasapyan CreditAttribution: Artyom Hovasapyan commentedFix bug.
Comment #41
thatguy CreditAttribution: thatguy at Siili Solutions commentedRe-rolled patch from #40 to work with dev-3.0.x version
Comment #42
kroh CreditAttribution: kroh commentedRe-rolled patch from #40 to work with 2.0.x
Comment #43
gwvoigtThanks, patch from #41 working on 3.x for me
Comment #44
scotwith1tPatch needs a re-roll for new 3.0.0 release.
Comment #45
scotwith1tTaking a stab...this is NOT my strong point, apologies if this is all messed up.
Comment #46
phma CreditAttribution: phma at OM commentedRe-roll of #44 for 2.1.x
Comment #47
phma CreditAttribution: phma at OM commentedComment #48
billywardrop CreditAttribution: billywardrop as a volunteer and at The University of Edinburgh commentedI patched groups 3.1 using the patch file in comment #46 but this stopped my theme running due to an error stating that group_content entity doesn't exist. Groups 3 now uses the group_relationship entity so I changed the entity type to group_relationship and the group nodes now show group blocks.
I have create a new patch and interdiff for this.
Comment #49
Ruslan PiskarovImproved #48. Just removed "+ + " in diff.
Comment #50
heddnWhy are we focused on Nodes? Why not just entity interface instead? then it will work for anything that group itself supports.
Comment #51
camslice CreditAttribution: camslice commentedReroll #49 for 2.2.x — for people on the upgrade path. My first patch, sorry if I messed it up.
Comment #52
banoodle CreditAttribution: banoodle at Kanopi Studios commentedPatch #51 applied OK for me, but throws these errors due to the inclusion of group_content.
Drupal\Component\Plugin\Exception\PluginNotFoundException: The "group_content" entity type does not exist. in Drupal\Core\Entity\EntityTypeManager->getDefinition() (line 139 of /code/web/core/lib/Drupal/Core/Entity/EntityTypeManager.php).
This patch I'm uploading just replaces "group_content" with "group_relationship".
Comment #53
le72What is the state of Group v3 patch?
I did fresh Drupal 10 install with Layout builder and Group 3. The problem #35 is still there.