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.

General

Set default taxonomy field value with blueprints

Started by Alexander Kim 8 years ago · 10 replies · 1887 views
8 years ago

single-video.yaml:

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.

8 years ago

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

YAML
       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
8 years ago

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

last edited 04/10/18 by Alexander Kim
8 years ago

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

8 years ago

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?

8 years ago

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

8 years ago

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

8 years ago

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

8 years ago

Okay, I finally found the answer! It comes from https://github.com/getgrav/grav-plugin-admin/pull/1364:

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.

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

YAML
            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

8 years ago

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

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

8 years ago

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

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 85 13 hours ago
General · by pamtbaau, 18 hours ago
1 60 18 hours ago
General · by Andy Miller, 1 day ago
0 47 1 day ago
General · by Marcel, 12 months ago
6 350 5 days ago
General · by Duc , 6 days ago
3 44 5 days ago