Set default taxonomy field value with blueprints

single-video.yaml:

        options:
          type: tab

          fields:

            taxonomies:

              fields:

                # 
                # Overriding taxonomy field to required
                # 

                header.taxonomy:

                  fields:

                    .category:

                      default: Video

But it’s not working, is that even possible? So every time the user would create a new video page, taxonomy field would be pre-filled with default value.

I was having the same issue, and after digging around some I found an article on stackoverflow that had the solution. Here’s the relevant portion of one of my blueprints. You’ll notice that I’ve separated category and tag, whereas default.yaml had them lumped together under header.taxonomies. I hope this helps.

Jeff

       taxonomies:
          fields:
            header.taxonomy:
              unset@: true

            header.taxonomy.category:
              type: selectize
              label: Category
              classes: fancy
              default: 'It''s Your Community'
              validate:
                type: commalist

            header.taxonomy.tag:
              type: selectize
              label: Tag
              classes: fancy
              validate:
                type: commalist
2 Likes

Thanks, it worked. By the way, is unsetting header.taxonomy is necessary?

Strictly speaking, no, it doesn’t appear necessary to unset header.taxonomy. However, what you will see is that you will have duplicate Category and Tag fields on your form, with the originals displaying first and then the additional fields after that. I tried an experiment with this, and I found that even though the original Category and Tag fields were there and could be filled in, the values didn’t take. If you want to actually affect Category and Tag, you need to fill in the header.taxonomy.category and header.taxonomy.tag fields that you add to the blueprint. In the end, I think it would lead to less confusion if you just unset header.taxonomy.

Jeff

Do you know how to make new taxonomy fields to show up all the created categories, just like with the default one drop down list?

@heihachi88 you can try adding the taxonomies to your site.yaml file. There is a property there called taxonomies which by default has [category, tag]

Sorry, but you didn’t understand our problem, question was not about how to add categories on the site.

I don’t think you’re going to get the dropdown without using type: taxonomy rather than type: selectize. If there is some way to specify default values for type: taxonomy, then that’s the way to go. Unfortunately, I don’t know of a way to do that, which is what led to the solution I proposed. I’m still looking into this, so if I figure anything out, I’ll post it.

Jeff

Okay, I finally found the answer! It comes from Sign in to GitHub · GitHub

With this patch user can override in blueprint the taxonomy field to add custom taxonomy values in default and options attributes.
There is only one catch - when overriding default taxonomy field, with both default and options attributes, you need to add validation attribute with type: commalist, as shown in example below. Otherwise, after first save Grav will throw an error at top of the page Array to string conversion, and page won’t be saved. But after second try, it will be successful. I haven’t found why this happens, but adding validation fixes it. I’m guessing that it might be related to #1024.

taxonomies:
  fields:
    header.taxonomy:
      default:
        category: ['blog','page']
        tag: ['test']
      options:
        category: ['grav']
      validate:
        type: commalist

I tried it, and it worked! The only issue I had was with replacing the default value when extending the blueprint. I have two types of pages that I want end users to be able to create, with the difference being what the default category is set to. So, I created a blueprint for a page with the first default category, and then I just extended it and attempted to replace the default category for the second page. I tried various methods, but none worked. I kept ending up with the original default category and my new one. Finally, I just had to replace the entire header.taxonomy, and then I could set the default value properly. Here’s how I did that:

            taxonomies:
              fields:
                header.taxonomy:
                  replace@: true
                  type: taxonomy
                  label: PLUGIN_ADMIN.TAXONOMY
                  multiple: true
                  default:
                    category: ['Saturday Morning Conversation']
                  validate:
                    type: commalist

I hope this solves your issue.

Jeff

Thanks, they already merged that PR? Link shows nothing. Didn’t understand this part:

taxonomies:
  fields:
    header.taxonomy:
      default:
        category: ['blog','page']
        tag: ['test']
      options:
        category: ['grav']
      validate:
        type: commalist

What purpose of default here? You’re just listing all available categories and tags on the entire website?

This change seems to be merged into the latest release at the very least. That’s how I found it, actually. I was looking through files and found reference to the change. I have tried it with my own code, and it is working.

As for the default in the example, I believe they’re just saying this page will have two default values for category and one for tag. I would expect you could specify anything from no default values to as many as you like for each taxonomy type.

Jeff

1 Like