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.

Archive

Show only a specific taxonomy in a page blueprint

Started by Muut Archive 9 years ago · 6 replies · 2627 views
9 years ago

hello folks! is there a way to display only a specific taxonomy type in a page blueprint? Right now when I add the field type taxonomy it shows all the taxonomies available.

9 years ago

hy, i've created my own custom field this is the code:
create the file taxonomytype.html.twig in this path
/admin/themes/grav/templates/forms/fields/, I do this in my plugin folder but i thinc it works also in a theme.

TWIG
{% set parentname = field.name %}
{% set name = field.filter %}

    {% set value = array(data.value(field.name)|default([])) %}
    {% set sub_taxonomies = attribute(grav.taxonomy.taxonomy, name)|default([])|keys %}
    {% set list = []|merge(sub_taxonomies)|merge(value)|array_unique %}
    {% set field = {
        type: 'select',
        classes: 'fancy create',
        label: field.label|capitalize,
        name: parentname,
        multiple: true,
        options: list,
        style: field.style,
        selectize: {
            create: true
        }
    } %}

    {% include ['forms/fields/select/select.html.twig'] %}

and in the page blueprint

YAML
                        header.filters:
                          type: taxonomytype
                          filter: category
                          label: Set category filter
                          multiple: true
                          validate:
                            type: array

Hope that help!

9 years ago

hi, with this field, you can have a filed in blueprint with only the taxonomy that you need (in my example see "filter:category"). After you save the page, you can retrive the value that you can save like any other other grav field and use it to filter the pages you need in your twig template. I use it to get collection by page taxonomy, for example

TXT
page.collection({'items':{'@taxonomy':{'category': page.header.filter , }},'order': {'by': 'default', 'dir': 'asc'}}) %}

In my case
Hope that help

9 years ago

but you need a selectize that show you only a single taxonomy type or a select where you can select the taxonomy type available on the site?? my example works for the first purpose.
Sorry.. I have a doubt :)

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1282 9 years ago
Archive · by Muut Archive, 9 years ago
2 889 9 years ago
Archive · by Muut Archive, 9 years ago
2 4019 9 years ago
Archive · by Muut Archive, 9 years ago
1 2894 9 years ago
Archive · by Muut Archive, 9 years ago
3 1078 9 years ago