Help Accessing Group Slug
-
We have the following logic in v2, but it no longer works in v3
{{{ if posts.user.groups.length }}} {{{ each posts.user.groups }}} post-{posts.user.groups.slug} {{{ end }}} {{{ end}}}
Can we still access group slug like this? Did this change in v3?
-
On the topic page there is
groups
property on each user, the user's selected badges are displayed via theselectedGroups
property, so the below should work.{{{ if posts.user.selectedGroups.length }}} {{{ each posts.user.selectedGroups }}} post-{posts.user.selectedGroups.slug} {{{ end }}} {{{ end }}}
-
@baris in addition to my post above, does this mean we cannot access group slug through posts.user.groups.slug anymore (in topic page)? If so, I gues the only way to access that is by enabling group badge in ACP.
-
@Teemberland if you don't see it in
ajaxify.data.posts[0].user
you can't access it in the template.
I think that property isn't available since 2016 https://github.com/NodeBB/NodeBB/commit/d67c0e1b089645156f53e3894ddbf3d57c696bc3.On the topic page only the groups that the user selected in their profile page are returned. If you want to return all the groups the user is a member of you will need to add them in with a plugin.
-
@baris said in Help Accessing Group Slug:
https://github.com/NodeBB/NodeBB/commit/d67c0e1b089645156f53e3894ddbf3d57c696bc3
I see! we were probably using an old plugin in slick. I'll double check, but otherwise, I'll use SelectedGroups. Thanks!
-
No worries, if for some reason you need all the groups the user is a member of you can accomplish that in the hook
filter:posts.getUserInfoForPosts
but keep in mind this might slow down your topic pages depending on how many groups the users are members of.myPlugin.filterPostsGetUserInfoForPosts = async (hookData) => { const { users } = hookData; const userGroups = await groups.getUserGroups(users.map(u => u.uid)); users.forEach((u, index) => { u.allGroups = userGroups[index]; }); return hookData; }
This would create the
allGroups
property on users that shows all the visible groups. -