Query for tags within a structure field

Hi all,

I have a structure field with ingredients and I want it to propose already used terms. I tried it with using tags and querying for them (which worked outside the structure field) — however this does not work.

Could someone point me to my error?

Thanks!

          zutaten:
            label: Zutaten
            type: structure
            columns:
              zutat_anzahl:
                width: 1/3
              zutat:
                width: 2/3
            fields:
              zutat_anzahl:
                label: Anzahl
                type: text
              zutat:
                label: Zutat
                type: tags
                max: 1
                required: true
                options:
                  type: query
                  query: page.siblings.pluck("zutaten", ",", true)

That doesn’t work because you try to pluck from the structure field, but that won’t return anything useful.

See documentation here how to query from a structure field: Tags | Kirby CMS

1 Like

Ok, this makes sense. It works like this for a single page:

options:
  type: query
  query: page.zutaten.toStructure
  text: "{{ item.zutat }}"
  value: "{{ item.zutat }}"

But if I want to query also its siblings query:

query: page.siblings.zutaten.toStructure

I get an Error “get_class(): Argument #1 ($object) must be of type object, null given”

Right, then you need a custom page method that returns all values from all siblings; you cannot achieve this using the query language.

This is not valid, because you can only query a field from a single page, not a collection.

Could you tell me how do achieve this? I’m honestly lost here…

This should help, unless you also need help with the code inside the method as well. But basically, you would loop through the pages and return an array of options.

I took me a while and I came up with this:

/site/plugins/zutatenmethod/index.php:

<?php

function usedIngredients($page)
{
    $usedIngredients = [];
    foreach ($page->siblings() as $sibling) {
        $ingredients = $sibling->zutaten()->toStructure();
        foreach ($ingredients as $ingredient) {
            if (!in_array($ingredient->zutat(), $usedIngredients)) {
                $usedIngredients[] = $ingredient->zutat();
            }
        }
    }
    return array_map(function($ingredient) {
        return ['value' => $ingredient, 'text' => $ingredient];
    }, $usedIngredients);
}

Query in the blueprint:

type: query
query: usedIngredients($page)

/site/config/config.php:

<?php
return [
    'debug' => true,
    'plugins' => [
      'zutatenmethod'
    ]
];

But I get the following error in the panel:

get_class(): Argument #1 ($object) must be of type object, null given in file: /usr/local/var/www/kirby/kirby/src/Option/OptionsQuery.php line: 158

You have to create a page method like in the link I posted above, not a simple function.

This is superfluous.

It works! Thank you so much (also for you patience).