Display query text and value

Hi there, I have a list of categories grouped in a loop:

The category is assigned on the children’s page:

  locations:
      label: Location
      type: multiselect
      options: query
      max: 1 
      query:
        fetch: site.locations.toStructure
        text: "{{structureItem.location}}"
        value: "{{structureItem.abbreviation}}"

And as you can see the query is fetching another multiselect field in a different page:

        locations:
          label: Locations
          type: structure
          fields:
            location:
              label: Location Name 
              type: text
            abbreviation:
              label: Location City
              type: multiselect
              options: query
              query:
                fetch: site.cities.toStructure
                text: "{{ structureItem.abbreviation }}"
                value: "{{ structureItem.abbreviation }}"

Everything works fine, but once I print the value I would like to display “text” and “value”. (As displayed on Kirby panel).

    <?php
    $locations = $page->children()->listed()->groupBy('locations');
    foreach($locations as $loc => $items): ?>
        <?= $loc ?>
    <?php endforeach ?>

Is that possible?

You can get location name and city from the $site->locations() structure, in this case. However, I wonder why you are storing the abbreviation as value, which doesn’t make sense in my opionion, because a location is probably not unique.

$item = $site->locations()->toStructure()->findBy('abbreviation', $loc); // this will return the first item that fits if any (which might not be the one you are looking for if there are multiple items with the same location abbreviation, so the stored value should be unique).
if ($item) {
  echo $item->location() . '--' . $item->abbreviation();
}

(Don’t understand why the abbreviation is a multiselect either…)

Well, probably I did it wrong but I first defined which the cities are:


(just 2 normal text fields)

Then I defined the locations:


“location city” is fetching the “city abbreviation” field above.

This allows me to assign a Location and then automatically having a city associate it once the event is generated.

These tricky steps are needed to apply a filter that highlights the location in a specific city:

(Once again, I probably set it wrong)

Oh, ok, it’s just that your setup is irritating, because you are using multiselect fields, and I thought the locations were something like event locations. The multiselect field for the location abbreviations should therefore either be replaced by a select field or limited to max 1.

Other than that, what I wrote above should work.

Yes, I limited each multi-select to max: 1 .

I beg you pardon but I think I get it wrong as it is not working…

    <?php
    $locations = $page->children()->listed()->groupBy('locations');
    foreach($locations as $loc => $items): ?>
      <?php 
        $item = $site->locations()->toStructure()->findBy('abbreviation', $loc); 
      ?>
      <div class="btn-primary">
        <?= $item->location() . '--' . $item->abbreviation(); ?>
      </div>
    <?php endforeach ?>

$locations = $page->children()->listed()->groupBy('locations');
is the Multiselect field that is fetching the value from another page.

What does “is not working” mean? Do you get an error? No output?

We are talking about these pages with this field, right (and why do you say page, when you are fetching it from $site)

locations:
      label: Location
      type: multiselect
      options: query
      max: 1 
      query:
        fetch: site.locations.toStructure
        text: "{{structureItem.location}}"
        value: "{{structureItem.abbreviation}}"

So in your loop, $loc should be something like BO for Bologna?

What do you get if you dump $item?

Feel free to send me a link to the project or something to download if it is to complicated to sort out with words… :wink:

There are two problems:

  1. Your content stores the location (Cinema aperto), not the abbreviation (BO) in the example, so looks as if you changed your blueprint but then didn’t update your content accordingly. Therefore, no item is found when looking for abbreviation = cinema aperto-

  2. This code is a no-no:

You always have to wrap such stuff in an if statement (see my code above), because you cannot count on the item to exist when you call a method like abbreviation() etc. on it.

To get a deeper understanding, you might want to check out my intro to PHP OOP: A brief intro to object oriented programming in PHP | Kirby CMS