Nested structure fields collection

Hey,

I have a select box on another page that I want to query the items in a structure field as well as it’s nested structure field entries, but am having trouble.

Fields to query:

categories:
  label: Categories
  type: structure
  fields:
    name:
      label: Category name
      type: text
    subcategories: 
      label: Sub-Categories
      type: structure
      fields:
        name: 
          label: Sub-Category Name
          type: text

Query

  category:
    label: Category
    type: select
    options: query
    query:
      fetch: kirby.collection('categories')
      text: {{ stuctureItem.name }}
      value: {{ stuctureItem.name }}

Is a collection the right way to go about this? If so what would it look like?

Thank you!

Yes, something like that should work depending on what your collection returns. Could you post your collection, please?

The alternative would be a route that return the necessary data as JSON.

Hi @texnixe,

I’ve managed to get create an array from the data, but it’s looking like I need an object — how would I go about this?

<?php 

return function ( $pages ) {
  
  $categories = $pages->find('git')->categories()->toStructure();
  $collection = [];
  $collection['categories'] = [];

  foreach ( $categories as $category ) {
    // Add categories to array
    $collection['categories'][] = array (
      'name' => (string)$category->name(),
      'description' => (string)$category->description(),
      'parent' => $category->subcategoryToggle()->toBool(),
      'child' => false,
      'children' => $category->subcategories()->value()
    );

    // Add Subcategories to array
    $subcats = $category->subcategories()->toStructure();
    foreach ( $subcats as $subcat ) {
      $collection['categories'][] = array(
        'name' => (string)$subcat->name(),
        'description' => (string)$subcat->description()->kt(),
        'parent' => false,
        'child' => true
      );
    }
  }

  return $collection;
};

After much testing and fiddling, this seems to work!!

collections/categories.php

<?php 

return function ( $site, $kirby, $pages ) {
  
  // Create categories and subcategories collections
  $categories = $pages->find('git')->categories()->toStructure();
  $all_cats = new Collection;

  // Iterate through all subcategories and add to Subcat_coll
  foreach ( $categories as $category ) {
    
    // Exclude top-level Categories
    if ( $category->subcategoryToggle()->toBool() == false ) {
      $all_cats->append( $all_cats->count(), $category );
    }

    // Append subcategories if they exist
    $subcats = $category->subcategories()->toStructure();
    foreach ( $subcats as $subcat ) {
      $all_cats->append( $all_cats->count(), $subcat );
    }
  }

  return $all_cats;
};

Query

...
            options: query
            query:
              fetch: kirby.collection("categories")
              text: "{{ structureItem.name }}"
              value: "{{ structureItem.name }}"

:raised_hands: