How to query nested structure-field into multiselect-field

Hi,

I´m trying to query values from a nested structure-field into a multiselect-field like this:

Source with nested structure-field:

filterCategories:
  label: Filter-Kategorien
  type: structure
  fields:
    filterCategory:
      label: Filter-Kategorie
      type: text
    filterTags:
      label: Filter-Tags
      type: structure
      fields:
        filterTag:
          label: Filter-Tag
          type: text

My multiselect-field with the query:

projectTags:
  label: Tags
  type: multiselect
  options: query
  query: 
    fetch: site.page(site.index.template('projektdatenbank')).filterCategories.toStructure
    text: "{{ structureItem.filterCategory }}"
    value: "{{ structureItem.filterCategory }}"

This works and shows the filterCategories as expected, but I´d like to go deeper and query the filterTags (nested structure-field). I´m very grateful for any help, since I´m totally stuck on this one.

I think you can’t do this with a simple query. You could create a json encoded array you provide through a route.

What a pity, but thanks for the suggestion @texnixe. I´ll try that.

The route-solution works like a charm and has by the way other advantages in a multilanguage setup in this context. :+1:

1 Like

Ah, this would be exactly what I need. @tideg Do you have a pointer on how to approach the route solution? (I haven’t used routes yet.)

Sure @yatil. Based on my examples in the first post, I first built the route, which generates a JSON-File containing my wanted tags like this:

'pattern' => 'project-filter-tags.json',
'action' => function () {
	$tags = array();
	foreach (site()->visit(site()->index()-> ... ->toString(), 'de')->filterCategories()->toStructure() as $filterCategory) {
		foreach ($filterCategory->filterTags()->toStructure() as $filterTag) {
			$tagGerman = $filterTag->filterTagGerman()->toString();
			$tagEnglish = $filterTag->filterTagEnglish()->toString();
			$tags[$tagEnglish] = $tagGerman;
		}
	}
	return response::json($tags);
}

Then I changed my multiselect-field in the blueprint to query this JSON-file like this:

projectTags:
  label: Tags
  type: multiselect
  translate: false
  options: api
  api: 
    url: "{{ site.url('de') }}/project-filter-tags.json"
    text: "{{ item }}"
    value: '"{{ item.key }}":"{{ item }}"'

I have a two-language-setup (de/en), which makes the whole thing a little special. As you probably see, I force the editor to input both language-versions of the tags in the german panel-version. Therefore I locked the source-structure-field and the target multiselect-field in the english panel-version.

1 Like

@tideg Thank you very much, this is very helpful!