Search code examples
netlifynetlify-cms

Collections in Hugo data file using Netlify CMS


I'm trying to get my head round using Netlify CMS with Hugo ssg.

I use:

[email protected]
[email protected]

I have a simple netlify-cms config.yml with two collections: posts and authors.

  backend:
    name: github
    repo: sebhewelt/atlas
    branch: master

  display_url: https://mypage.com
  publish_mode: editorial_workflow  
  media_folder: "static/uploads"
  public_folder: "/uploads"
  collections:

    - label: "Posts"
      name: "post"
      folder: "content"
      create: true
      slug: "{{year}}-{{month}}-{{day}}-{{slug}}"
      fields:
        - { label: "Title", name: "title", widget: "string" }
        - { label: "Publish Date", name: "date", widget: "datetime" , format: "YYYY-MM-DD hh:mma"}
        - { label: "Body", name: "body", widget: "markdown" }


    - label: "Authors"
      name: "author"
      folder: "data"
      create: true
      fields:
        - {label: "Name", name: "name", widget: "string"}
        - {label: "About", name: "about", widget: "string"}

The docs distinguish two collection types, of which I assume i should choose file collection, as I'd like to hold the authors data in one file.

I'd like to be able to add authors via admin dashboard and save it to file in data folder. The docs doesn't provide an example of how should the file holding the authors look like (Or does the cms make it automatically?).

I encounter an error with my current config. When I'm saving the "New Author" i get this:

Failed to persist entry: Error: Collection must have a field name that is a valid entry identifier

Why do i get this error?


Solution

  • Your authors file needs to be under a top-level collection. Also, if you want to be able to add multiple authors to the file, you need to wrap the "name" and "about" widgets in a "list" type widget.

    Example:

      collections:
        - label: "Settings"
          name: "settings"
          files:
            - name: "authors"
              label: "Authors"
              file: "data/authors.yml"
              extension: "yml"
              fields:
                - label: "Author"
                  name: "author"
                  widget: "list"
                  fields:
                    - {label: "Name", name: "name", widget: "string"}
                    - {label: "About", name: "about", widget: "string"}
    

    CMS docs for file collections: https://www.netlifycms.org/docs/collection-types/#file-collections

    CMS docs for list widgets: https://www.netlifycms.org/docs/widgets/#list