2

If I’m finding all the entries that are related to a user, how could I get the field handle that the found user is referenced from?

{% set events = craft.entries.section('events').relatedTo(user) %}
{% for event in events %}

  {# What goes here? Hypothetical code based on non-existent method would be: #}

  {% set field = craft.fields.relatedTo(user) %}
  {{ field.title }}

{# Obviously this isn't possible. Any way to accomplish something like this? #}

{% endfor %}

There are multiple users fields in the found entries, and there will be different logic depending on which field the user is found.

4

What could work for you is to get the events for each field separatly:

{% set firstEvents = craft.entries.section('events').relatedTo({
    element: user,
    field: 'firstEntriesField'
}) %}

{% set secondEvents = craft.entries.section('events').relatedTo({
    element: user,
    field: 'secondEntriesField'
}) %}

If you need them combined in one loop, I can't think of another way other than to query x + 1 times:

{% set allEvents = craft.entries.section('events').relatedTo({
    element: user,
}) %}

{% set firstEventsIds = craft.entries.section('events').relatedTo({
    element: user,
    field: 'firstEntriesField'
}).ids() %}

{% set secondEventsIds = craft.entries.section('events').relatedTo({
    element: user,
    field: 'secondEntriesField'
}).ids() %}

{% for event in allEvents %}
    {% if event.id in firstEventsIds %}
        {# Event related through firstEntriesField #}
    {% elseif event.id in secondEventsIds %}
        {# Event related through secondEntriesField #}
    {% endif %}
{% endfor %}
|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.