Category collection / popup in Jekyll

Is it possible to have a ‘collection’ of categories (for taxonomy) which I can add/edit/delete, and then have those populate a popup when creating a post, rather than free text entry?

I’m confused because this list of categories would not be files … just an array of strings. Where would that get “stored”?

Thanks!

Have a look at the relation widget. You can set multiple to true or use it in combination with a list widget

1 Like

Yes, I looked at those … but not clear how to combine relation with a collection – since collections are only file or folder. Should I create a folder of ‘dummy’ files which have the names of the categories? I want it to be like Wordpress, where in one place you define/edit a list of categories, and then those populate the options of a select widget. thanks

You could create a Settings file collection, with a categories list:

collections:
  - name: settings
    label: Settings
    editor:
      preview: false
    files: 
      - name: blog_settings
        label: Blog settings
        file: data/blog_settings.yml
        fields: 
          - name: categories
            widget: list
            summary: '{{fields.name}}'
            fields:
              - {name: name}

Which you can then target in your post collection:

collections:
  - ...

  - name: posts
    label: Posts
    folder: data/posts/
    create: true
    slug: '{{title}}'
    editor:
      preview: false
    fields:
      - {label: Title, name: title, widget: string}
      - label: Category
        name: category
        widget: relation
        collection: settings
        file: blog_settings
        value_field: categories.*.name
        search_fields: [categories.*.name]
        display_fields: [categories.*.name]

The biggest advantage of this approach is that if you have multiple of these relation widgets (say… tags, authors, etc), you can all put them in one file together.

Very cool! That worked.
I think you have a typo… file: blog__settings should have just 1 underscore, i.e., file: blog_settings

FWIW … My proposed solution of using a folder collection with files also worked:

with the advantage that I don’t have to dig into Settings > Blog Settings.

It comes from files in this folder:

categories/map-category-cat1.md
categories/map-category-cat3.md
categories/map-category-reviews.md

Just wondering where the map-category- prefix comes from when I create a new category? Could the generated file just be the category name + .md ?

My config:

collections:
...
  - name: categories
    label: Categories
    folder: categories
    create: true
    fields:
      - widget: string
        name: category
        label: Category
        required: true
...
    fields: 
...
      - label: "Categories"
        name: "categories"
        widget: "relation"
        required: false
        multiple: true
        collection: categories
        value_field: category
        search_fields: ['name']

Yes, that certainly would work. I was under the impression that you were trying to avoid a file per category kinda set-up.

Anyway: Not sure why that map prefix occurs, but try setting a slug or alternatively an identifier field for your categories collection:

  - name: categories
    label: Categories
    folder: categories
    identifier_field: category
    slug: "{{fields.category}}"
    create: true
    fields:
      - widget: string
        name: category
        label: Category
        required: true

You can learn more about all the different configuration settings for collections here: