Filtering structure fields by their own field values

Hi,

I have the following code in my blueprint:

stockists:
    label: Stockists
    type: structure
    entry: >
        {{name}}<br>{{address}}<br>{{website}}
    fields:
        name:
            label: Name
            type: text
        city:
            label: City
            type: text
        website:
            label: Website
            type: url

and I want to get the following code my template

<ul>
<li>
    <h2>New York</h2>
    <ul>
        <li>
            <a href="http://some-website.com">
                Some Stockist
            </a>
        </li>
        <li>
            <a href="http://some-website.com">
                Some Stockist
            </a>                                
        </li>
        <li>
            <a href="http://some-website.com">
                Some Stockist
            </a>                                
        </li>
        <li>
            <a href="http://some-website.com">
                Some Stockist
            </a>                                
        </li>
    </ul>
</li>
<li>
    <h2>Tokyo</h2>
    <ul>
        <li>
            <a href="http://some-website.com">
                Some Stockist
            </a>
        </li>
        <li>
            <a href="http://some-website.com">
                Some Stockist
            </a>                                
        </li>
        <li>
            <a href="http://some-website.com">
                Some Stockist
            </a>                                
        </li>
        <li>
            <a href="http://some-website.com">
                Some Stockist
            </a>
           ...

Essentially i want to get the available cities within the available structure field entries and then use those to group the entries by city.

Any ideas on how this could be done?

Thanks in advance

Yes, you can group by that field:

<?php
$stockists = $page->stockists()->structure()->groupBy('city');
foreach($stokists as $key =>$stockist ):
  echo $key;
  foreach($stockist as $details):
    echo $details->name();
  endforeach;
endforeach;
1 Like

Exactly what I’ve been looking for! Thank you so much!