2

This is so simple in ExpressionEngine and yet I've tried everything for several hours and can't find a way to get it working in Craft???

I have 2 User Groups: userGroup1, userGroup2

If the currently logged-in User belongs to either of those groups, show them "==SECRET CONTENT==" that guests (not logged-in users) do not see.

The following IF will only show "==SECRET CONTENT==" to userGroup1:

{% if currentUser and currentUser.isInGroup('userGroup1') %}
    ==SECRET CONTENT==
{% endif #}

Now, based on everything I've found and read, it would seem the following SHOULD show ==SECRET CONTENT== to Users belonging to EITHER userGroup1 OR userGroup2:

{% if currentUser and currentUser.isInGroup('userGroup1','userGroup2') %}
    ==SECRET CONTENT==
{% endif #}

But it only shows ==SECRET CONTENT== to userGroup1. And if I switch the isInGroup values to the following, only userGroup2 see the ==SECRET CONTENT==

{% if currentUser and currentUser.isInGroup('userGroup2','userGroup1') %}
    ==SECRET CONTENT==
{% endif #}

I've tried Group IDs. I've tried every variation of formatting, array syntax, etc. I've searched the docs and StackExchange dozens of times...

How can I display "==SECRET CONTENT==" to users belonging to userGroup1 OR userGroup2 ??

  • A user is always in 1 group right? Wouldn't twig syntax like {% if currentUser.group in ['usergroup1','usergroup2'] %} work? – T.Skye Sep 3 '16 at 13:57
  • @T.Skye: Not necessarily... A user can belong to multiple groups. – Lindsey D Sep 3 '16 at 14:58
  • @T.Skye: That's exactly what I am trying/hoping to do... But the currentUser global link is a UserModel object and its getGroups method link returns an array of Groups that user belongs to... Then I'd have to figure out how to match any of the user's groups against the groups I'm testing for... It got convoluted and I hoped there was an easier syntax...like you offered. But I can't find it and Mats solution below works for now until I understand Craft and Twig better. :) – Shane Robinson Sep 3 '16 at 19:43
6

Going to chime in here as I ran into a similar issue, and solved it a bit differently.

Instead of using group handles I was using a dynamic set of Group IDs. However, the problem still remains that isInGroup() doesn't support an array of handles or ids. So you gotta run some checks.

And, I think you can be little more dynamic using IDs.

{% set coolArrayOfGroupIds = [2, 4, 5, 400] %}

{% if currentUser %}
    {% set allowedAccess = false %}

    {% for group in currentUser.getGroups() %}
        {% if group.id in coolArrayOfGroupIds %}
            {% set allowedAccess = true %}
        {% endif %}
    {% endfor %}

    {% if currentUser.admin or allowedAccess %}
        {# You win! Secrets Time #}
    {% endif %}

{% endif %}
| improve this answer | |
  • Even better and closer to what I was looking for! Thanks so much, @Ian!! – Shane Robinson Sep 29 '16 at 16:44
9

According to the docs, the userModel.isInGroup() method takes a "user group model, its handle, or ID" – in other words, it can only test for membership in a single group, not several.

What you want in this case is the or operator:

{% if currentUser and (currentUser.isInGroup('userGroup1') or currentUser.isInGroup('userGroup2')) %}
    {# secret content #}
{% endif %}
| improve this answer | |
  • Mahalo (thank you) @Mats, that does indeed work! And I swear I tried that....but I also swear that I read the isInGroup() method takes an array in addition to the group model, handle, or id... But now I can't find that either? :-) – Shane Robinson Sep 3 '16 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.