1

I am having trouble referencing a user in an if statement:

{% set otherEntry = craft.entries().section('other').userFieldInOtherEntry("{externalVariable}").one() %}

{% if currentUser is relatedTo(otherEntry) %}
    do something
{% endif %}

I've tried using the for loop method described in the Craft 3 documentation, but I get a recursive loop error.

2

You could do:

{% set otherEntry = craft.entries().section('other').userFieldInOtherEntry("{externalVariable}").one() %}

{% if currentUser in otherEntry.userFieldInOtherEntry %}
    do something
{% endif %}
| improve this answer | |
  • Thank you Oli, I'll try this solution out and see how it works. – JJefferyDev Jan 27 at 17:20
  • This suggestion didn't work. I'll post what ended up working for me. – JJefferyDev Jan 27 at 19:34
0

I ended up having to go through a for loop checking if the user is related to the current entry, then if statements inside of that for loop. Is there a cleaner way to do this?

{% if currentUser.isInGroup('userGroup') %}
<p>My Things:</p>
{% set relatedThings = craft.entries.section('things').relatedTo(currentUser) %}
<ol>
    {% for rel in relatedThings if rel.createdFieldinThing == externalVariable %}
    <li>{{rel.title}} - {{rel.createdFieldinThing}}
        {% if rel.createdFieldinThing == externalVariable %}
                the current user is in the thing
            {% else %}
                the current user is not in the thing
            {% endif %}
        {% endfor %}
    </li>
</ol>
{% endif %}
| improve this answer | |

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.