How to show taxonomy entries as select options in a blueprint

@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