<p>The group filter creates an associative array so the location Twig var in your loops is the array item key, which is a string. </p>

<p>I think you'll have to get the location object that's related to the entry you're currently working with in the loop in order to access its URL. Maybe try setting the location object to a variable by adding something like <code>{% set locationObj = entriesInLocation[0].location.one() %}</code> right after <code>{% for location, entriesInLocation in allEntriesByLocation %}</code>. Then, you should be able to access the location URL using <code>{{ locationObj.url }}</code>.</p>

CC BY-SA 4.0
2020-05-28T09:03:54.350Z
2020-05-28T09:03:54.350Z
Mark Northrop
<p>So I am trying to group entries by location and then group entries in this group by weekday.
I have this aspect working with the following code:</p>

<pre><code>{% set allClasses = craft.entries
  .section('tuitionClasses')
  .relatedTo({
  targetElement: entry,
  field: 'tuitionCourse'
})
  .all() %}

{% set allEntriesByLocation = allClasses|group(e =&gt; e.location.one.title) %}


&lt;div class="md:flex md:flex-wrap max-w-7xl mx-auto"
"&gt;
&lt;div class="md:w-1/4"&gt;
  &lt;ul class="my-6"&gt;
    {% for location, entriesInLocation in allEntriesByLocation %}
      &lt;li
        :class="{ 'bg-gray-50': tab === '{{ loop.index }}' }"
        class=" text-gray-700 mb-3"
      &gt;
        &lt;button
          class="focus:outline-none p-4 px-6"
          :class="{ 'text-pink-600': tab === '{{ loop.index }}' }"
          @click="tab = '{{ loop.index }}'"
        &gt;
          &lt;h2 class="text-2xl font-medium mb-0"&gt;{{ location }}&lt;/h2&gt;
        &lt;/button&gt;
      &lt;/li&gt;
    {% endfor %}
  &lt;/ul&gt;
&lt;/div&gt;

&lt;div class="md:flex-1 bg-gray-50 p-12"&gt;
  {% for location, entriesInLocation in allEntriesByLocation %}
    &lt;div x-show="tab === '{{ loop.index }}'"&gt;
      {#&lt;h2 class="text-gray-700 text-2xl font-medium"&gt;{{ location }}&lt;/h2&gt;#}


      {% set locEntriesByWeekday = entriesInLocation|group(i =&gt; i.weekday) %}

      &lt;div class="grid grid-cols-3 gap-6 grid-flow-col-dense"&gt;
        {% for weekday, entriesForWeekday in locEntriesByWeekday %}
          &lt;div&gt;
            &lt;h3 class="text-pink-600 text-md font-medium mb-3"&gt;{{ weekday }}&lt;/h3&gt;
            &lt;ul&gt;
              {% for class in entriesForWeekday %}
                &lt;li class="flex items-center mb-3 ml-3"&gt;
                  {{ svg('@icons/bx/bx-time.svg', class="w-5 h-5 mr-1 flex-shrink-0 fill-current") }}
                  {{ class.classTime.one.title }}
                &lt;/li&gt;
              {% endfor %}
            &lt;/ul&gt;
          &lt;/div&gt;
        {% endfor %}
      &lt;/div&gt;

      &lt;div class="pt-6 -mb-6"&gt;
        &lt;button
          @click="html='loading...'; showLoading = true; showModal = !showModal;
              fetch('{{ entry.url }}', {
                method: 'GET',
                headers: {
                'X-Requested-With': 'XMLHttpRequest',
                },
              })
              .then(response =&gt; response.text())
                .then(text =&gt; {
                html = text
                showLoading = false;
              })"
          class="flex items-center justify-end text-left"
        &gt;
          &lt;div class="bg-gray-600 h-8 w-8 rounded-2 p-1 text-white flex-shrink-0 mr-2 // flex flex-col items-center justify-center"&gt;
            {{ svg('@icons/bx-solid/bxs-map.svg', class="p-px w-full h-full flex-shrink-0 fill-current") }}
          &lt;/div&gt;
          &lt;div&gt;
            &lt;span class="block font-medium text-base text-gray-700 leading-tight"&gt;
            {{ location.url() }}
            &lt;/span&gt;
            &lt;span class="block text-xs text-pink-600 leading-tight"&gt;
              Map and Directions
            &lt;/span&gt;
          &lt;/div&gt;
        &lt;/button&gt;
      &lt;/div&gt;
    &lt;/div&gt;
</code></pre>

<p>However, where I am stuck is accessing the url for the locations.
I have a button that should load the location page into a modal when clicked. </p>

<p>However, when I try <code>{{ location.url() }}</code> I get the following error.</p>

<pre><code>Impossible to invoke a method ("url") on a string variable ("Hatch End").
</code></pre>

<p>So How can I get any other element details of the location via this approach?
Or is there a different approach I need to take?</p>

<p>Thanks.</p>

<templating><group>
Stuck when grouping entries to get any other parameters like url, id etc
# Stuck when grouping entries to get any other parameters like url, id etc

(Asked by: username_0 on 2020-05-28)

So I am trying to group entries by location and then group entries in this group by weekday.
I have this aspect working with the following code:
{% set allClasses = craft.entries
  .section('tuitionClasses')
  .relatedTo({
  targetElement: entry,
  field: 'tuitionCourse'
})
  .all() %}

{% set allEntriesByLocation = allClasses|group(e => e.location.one.title) %}




  
    {% for location, entriesInLocation in allEntriesByLocation %}
      
        
          {{ location }}
        
      
    {% endfor %}
  



  {% for location, entriesInLocation in allEntriesByLocation %}
    
      {#{{ location }}#}


      {% set locEntriesByWeekday = entriesInLocation|group(i => i.weekday) %}

      
        {% for weekday, entriesForWeekday in locEntriesByWeekday %}
          
            {{ weekday }}
            
              {% for class in entriesForWeekday %}
                
                  {{ svg('@icons/bx/bx-time.svg', class="w-5 h-5 mr-1 flex-shrink-0 fill-current") }}
                  {{ class.classTime.one.title }}
                
              {% endfor %}
            
          
        {% endfor %}
      

      
        
          
            {{ svg('@icons/bx-solid/bxs-map.svg', class="p-px w-full h-full flex-shrink-0 fill-current") }}
          
          
            
            {{ location.url() }}
            
            
              Map and Directions
            
          
        
      
    

However, where I am stuck is accessing the url for the locations.
I have a button that should load the location page into a modal when clicked. 
However, when I try {{ location.url() }} I get the following error.
Impossible to invoke a method ("url") on a string variable ("Hatch End").

So How can I get any other element details of the location via this approach?
Or is there a different approach I need to take?
Thanks.

**Tags:** templating, group

---

## 1 answers

### Answer by username_1 on 2020-05-28. Score: 2 | Accepted answer

The group filter creates an associative array so the location Twig var in your loops is the array item key, which is a string.

I think you'll have to get the location object that's related to the entry you're currently working with in the loop in order to access its URL. Maybe try setting the location object to a variable by adding something like `{% set locationObj = entriesInLocation[0].location.one() %}` right after `{% for location, entriesInLocation in allEntriesByLocation %}`. Then, you should be able to access the location URL using `{{ locationObj.url }}`.


---

