Fetching a collection in a select field

Hello,

I try to query a collection as options for a select field. It seems that the query correctly fetches the collection since the select field displays the correct number of items but the texts and values are blank.

The collection :

<?php

return function ($site) {
  $blockTemplates = [];
  $pages = $site->index();
  foreach ($pages as $page) {
    if ($page->composition()->isNotEmpty()) {
      $blocks = $page->composition()->toBlocks();
      foreach ($blocks as $block) {
        if ($block->isTemplate()->value() == 'true') {
          $blockTemplate = $block;
          $blockTemplates[] = $blockTemplate;
        }
      }
    }
  }
  return $blockTemplates;
};

The field :

  template:
    type: select
    options: query
    query:
      fetch: kirby.collection('block-templates')
      text: "{{ arrayItem.templateName }}"
      value: "{{ arrayItem.templateName }}"

The result :

Thank you for your help !

Your collection returns an array of block objects, therefore the text/value pairs will not work. What exactly do you want to get here?

It’s a sort of experimentation and my goal was too ambitious. Its enough for the moment to only get the $block->templateName() and it works by extracting it within the collection function :

return function ($site) {
  $blockTemplates = [];
  $pages = $site->index();
  foreach ($pages as $page) {
    if ($page->composition()->isNotEmpty()) {
      $blocks = $page->composition()->toBlocks();
      foreach ($blocks as $block) {
        if ($block->isTemplate()->value() == 'true') {
          $blockTemplate = (string)$block->templateName();
          $blockTemplates[] = $blockTemplate;
        }
      }
    }
  }
  return $blockTemplates;
};
  useTemplate:
    label: Utiliser un template
    type: select
    options: query
    query: kirby.collection('block-templates')
    when:
      isTemplate: false

Thank you for your answer