How to show taxonomy entries as select options in a blueprint

Hey everybody,

I would like to add taxonomy entries to a select field in a page blueprint. Much like the example from the docs where they use a function call to list page routes, just with taxonomies. But I can’t figure out the function to use (here’s the relevant API section). I tried:

header.events_tag:
  type: select
  label: Schlagwort der zu zeigenden Termine
  data-options@: '\Grav\Common\Taxonomy::getTaxonomyItemKeys("categories")'
  options:
    '--': '- zeige alle Termine -'

and also \Grav\Common\Taxonomy::findTaxonomy("categories"), but neither returns anything. I put something in the categories of course…

Does anybody know how it’s done correctly? Your help is much appreciated!

@Netzhexe, When a theme/plugin is being called by Admin, pages are not being initialized. Hence, taxonomies are not being collected…

Admin needs to be forced to load the pages using $admin->enablePages()

What you could do (see also this post):

  • Create a static function in theme/plugin:
    public static function taxonomyValues(string $taxon)
    {
      /** @var Grav */
      $grav = Grav::instance();
    
      /** @var Admin */
      $admin = $grav['admin'];
      $admin->enablePages();
    
      /** @var Taxonomy */
      $taxonomy = $grav['taxonomy'];
    
      $keys = $taxonomy->getTaxonomyItemKeys($taxon);
      $values = [];
    
      foreach ($keys as $key) {
         $values[$key] = $key;
      } 
    
      return $values;
    }
    
  • And call the static function in the blueprint using:
    data-options@: ['\Grav\Theme\MyTheme::taxonomyValues', 'category']
    --or--
    data-options@: ['\Grav\Plugin\MyPluginPlugin::taxonomyValues', 'category']
    
    Note when using a plugin, the name must end with 'Plugin .
1 Like