2

I'm working with a Structure of resort locations. Each resort has a field address which is a SmartMap field. I'm trying to create an initial display page that groups the resorts based on their state (a SmartMap address part), but I would like to order the output first based on the state and then by resort title.

Something like.

  • Arkansas
    • Awesome Place
    • Best Place
  • Alabama
    • Alright Place
    • Better Place
  • Florida

I'm partially there using the following:

{% set resorts = craft.entries().section('resortsList').orderBy('title ASC').all() %}
{% set resortsByState = resorts|group('address.state') %}

And then outputting the list...

{% for state, resortsInState in resortsByState %}
  <h2>{{ state }}</h2>
  {% for resort in resortsInState %}
    <p>{{ resort.link }} - {{ resort.address.state }}</p>
  {% endfor %}
{% endfor %}

This gives me the list grouped by the state and ordered by the resort title which gets me partially there. I've tried adding address.state as part of the ordering declaration, but I'm only getting errors (Unknown column 'address.state' in 'order clause'). Is there a proper way I should be doing this?

2

Okay, I've been able to solve this and figured I'd share what I discovered. The key for each group is the value of the state so really what I was after here was a key sort and just didn't realize it. So I found Michael Rog's "SuperSort" plugin which has a twig filter for running a ksort and that addressed it perfectly for me making a small adjustment to use supersort in the response.

{% set resorts = craft.entries().section('resortsList').orderBy('title ASC').all() %}
{% set resortsByState = resorts|group('address.state')|supersort('ksort') %}

I'm interested in other solutions too that don't require a plugin, but this particular direction got things going for me.

  • Nice work! Was going to respond and say exactly this. :) – Lindsey D Dec 20 '18 at 21:50
  • 1
    There's no other solution at the moment. The address data isn't available as part of the initial SQL query, so you need to handle it separately. – Lindsey D Dec 20 '18 at 21:51
  • 1
    Okay awesome, thank you so much Lindsey for the confirmation! – Tim Knight Dec 20 '18 at 21:53

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.