1

I have a section for Businesses. I also have categories that these businesses can belong to. On each category page I want to list all the businesses that belong to that category. So far so good, but here's the rub: Each business also has an entry field that relates the business to a Town entry.

Businesses belong both to a category and a town entry.

On the category page I want to list the businesses by towns. So instead of having a laundry list of businesses, the businesses would be broken up by the different towns they belong to:

Town A
    - Business 1
    - Business 2
Town B
    - Business 3
Town C
    - Business 4
    - Business 5
    - Business 6

I am grabbing my businesses like this:

{% set businesses = craft.entries.relatedTo(category) %}

I have seen other answers to different questions suggest GROUP, but I'm not sure how to correctly go about using the group method on an array when I am trying to group via another array (entry field).

I tried this, but it results in "Object of class Craft\ElementCriteriaModel could not be converted to string":

{% set businessesByTown = businesses|group('bizTown') %}

I know that's fishy and incorrect, but I don't know how to accomplish it the right way. bizTown is the entry field I want to sort by.

If anyone could help, that'd be wonderful! Thank you!

2

You are getting this error because the "entries" fields are always considered to be a query (ElementCriteriaModel) that can return more than one element. Therefore, if you know for sure that you will always have one and only one town linked to a business, you could do something like that:

{% set businessesByTown = businesses|group('bizTown[0].title') %}

If the title field is the name of the town you should get an associative array with the town names as keys and the businesses entries as values.

Additional info about sorting, following swthate's comment below:

If you want to sort the businesses, I think it can be done in the original query, so it would become:

{% set businesses = craft.entries.relatedTo(category).order('title') %}

However, the towns cannot be sorted directly since Twig's sort filter uses asort which sorts the array based on values. What you can do then is extract the businessesByTown array's keys in another array:

{% set towns = businessesByTown|keys|sort %}

You now have a sorted list of towns. Using this list, you can loop on all towns and businesses like that:

{% for town in towns %}
    {{ town }}
    {% set townBusinesses = businessesByTown[town] %}
    {% for business in townBusinesses %}
        {{ business.title }}
    {% endfor %}
{% endfor %}
  • Awesome, it works great. Thanks for the clear description! – Steven Thate Jun 16 '17 at 14:55
  • 1
    One quick follow-up, if I wanted to order both businesses and towns alphabetically, where would I put .order('title')? – Steven Thate Jun 16 '17 at 15:10
  • Updated my answer following your comment. – kant312 Jun 16 '17 at 15:29

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.