Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Forms & Blueprints

How to show taxonomy entries as select options in a blueprint

Started by Anna 4 years ago · 1 replies · 526 views
4 years ago

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:

YAML
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!

4 years ago

@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:

    PHP
    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:
    TXT
    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

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1139 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 63 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 137 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 112 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 131 7 months ago