User Group - accessing names and handles. #14942
-
Currently recording hard coded user group IDs to be saved with entries that I then use to retrieve based on a users group by searching a text field looking for corresponding ID, all is good the world.
All info uses user.groups to restrict viewing. Cannot find any documentation about accessing user group names and handles. If I rename a group or introduce more, I'd like to access the names programmatically. I've found previous discussions but the code used doesn't seem to exist in v4 https://craftcms.stackexchange.com/questions/20575/how-to-list-all-user-groups-and-users |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You should generally be checking groups based on their handles, rather than IDs. You don’t have any control over user groups’ IDs, and they could change from environment to environment.
Is this for a user query? If so you can use the {% set users = craft.users()
.group(['groupA', 'groupB'])
.all() %}
{% for group in user.groups %}
{{ group.name|t }}
{% endfor %} You could also call {% if user.isInGroup('groupA') %}
...
{% endif %} |
Beta Was this translation helpful? Give feedback.
You should generally be checking groups based on their handles, rather than IDs. You don’t have any control over user groups’ IDs, and they could change from environment to environment.
Is this for a user query? If so you can use the
group
param to pass in the user group handles you care about: