3

I have a relationship between users and 'locations', and I'm using the code below to output some markers for a map.

This all works fine except for two things:

  1. I'm repeating a couple of 'for' loops. When I try not to repeat them, the 'markers' and 'markerInfo' return 'undefined'.
  2. Within markerInfo, I'd like to list the names (e.g. 'user.firstName') of the users related to that location, but when I try, I get a list of ALL users that have a relationship with ANY location.

In plain language, what I'm trying to do is:

  • Find me all the users who are part of the 'family' group.
  • Find me all the locations which are related to those users and plot each one (but only once for each location).
  • For each location, give me a list of the users that have a relationship with it.

--

  • My source elements are entries in the section 'locations', with the field 'relatedPeople'.
  • My target elements are 'users' (from the group 'family').

How can I make this more performant (not repeat things), and get a list of users per location within 'markerInfo'?

{% set people = craft.users.group('family') %}

{% set markers %}
    {%- for person in people.all() -%}
        {% set relatedPlaces = craft.entries.section('locations').relatedTo({
            targetElement: person,
            field: 'relatedPeople'
        }) %}
        {%- for place in relatedPlaces.all() -%}
        ['{{ place.location.address }}', {{ place.location.lat }},{{ place.location.lng }}],
        {%- endfor -%}
    {%- endfor -%}
{% endset %}

{% set markerInfo %}    
    {%- for person in people.all() -%}
        {% set relatedPlaces = craft.entries.section('locations').relatedTo({
            targetElement: person,
            field: 'relatedPeople'
        }) %}
        {%- for place in relatedPlaces.all() -%}
        ['<div class="info_content">' + '<h5>{{ place.title|e('js') }}</h5>' + '{% if place.startDate|length %}{{ place.startDate | date('M Y') }}{% endif %}{% if place.startDate|length and place.finishDate|length%} to {% endif %}{% if place.finishDate|length %}{{ place.finishDate | date('M Y') }}{% endif %}' + '</div>'],
        {%- endfor -%}
    {%- endfor -%}
{% endset %}
3
{% set people = craft.users.group('family').ids() %}
{% set relatedPlaces = craft.entries.section('locations').relatedTo({
    targetElement: person,
    field: 'relatedPeople'
}) %}

Grab all the ids from your users, then find all locations related to those. This way you don't need multiple loops and get each location once and not multiple times

| improve this answer | |
  • This didn't work, but using the ids got me on the right track - thank you! – Adam George Jun 25 '18 at 10:38
2

With help from Robin (getting the ids), this article from Straight Up Craft, and a bit of trial and error, I ended up solving my issue with this:

{% set people = craft.users.group('family').ids() %}
{% set relatedPlaces = craft.entries.section('locations').relatedTo({
    targetElement: people,
    field: 'relatedPeople'
}) %}

{% set markers = '' %}
{% set markerInfo = '' %}   

{%- for place in relatedPlaces.all() -%}
    {% set relatedPeople = craft.users.group('family').relatedTo(place) %}
    {%- set markers -%}
        {% if not loop.first %}{{ markers }}{% endif %} ['{{ place.location.address }}', {{ place.location.lat }},{{ place.location.lng }}],
    {%- endset -%}
    {%- set markerInfo -%}
        {% if not loop.first %}{{ markerInfo }}{% endif %} ['<div class="info_content">' + '<h5>{{ place.title|e('js') }}</h5>' + '{%- for person in relatedPeople -%}{{ person.firstName }} {{ person.lastName }}<br>{%- endfor -%}' + '</div>'],
    {%- endset -%}
{%- 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.