Trying to get relation widget to work with file in nested collection

Hi all,

I’m struggling to get the relation widget to work for a list of authors that’s kept in a YAML file in a “Settings” collection, similar to the setup here: https://github.com/netlify/netlify-cms/blob/master/dev-test/config.yml

I’ll post what I have, it looks very similar to the config.yml above since that’s how I’ve been doing dev & test locally:

collections:
  - name: 'faq' 
    label: 'FAQ'
    folder: '_faqs'
    create: true 
    fields: 
      - { label: 'Question', name: 'title', widget: 'string', tagname: 'h1' }
      - { label: 'Answer', name: 'body', widget: 'markdown' }
      - {
          label: "Author",
          name: "author",
          widget: "relation",
          collection: "settings",
          searchFields: ["authors.authors.name"],
          displayFields: ["authors.authors.name"],
          valueField: "authors.authors.name",
          required: false
        }

  - name: 'settings'
    label: 'Settings'
    delete: false # Prevent users from deleting documents in this collection
    editor:
      preview: false
    files:
      - name: 'general'
        label: 'Site Settings'
        file: '_data/settings.json'
        description: 'General Site Settings'
        fields:
          - { label: 'Global title', name: 'site_title', widget: 'string' }
          - label: 'Post Settings'
            name: posts
            widget: 'object'
            fields:
              - {
                  label: 'Number of posts on frontpage',
                  name: front_limit,
                  widget: number,
                  min: 1,
                  max: 10,
                }
              - { label: 'Default Author', name: author, widget: string }
              - { label: 'Default Thumbnail', name: thumb, widget: image, class: 'thumb', required: false }

      - name: 'authors'
        label: 'Authors'
        file: '_data/authors.yml'
        description: 'Author descriptions'
        fields:
          - name: authors
            label: Authors
            label_singular: 'Author'
            widget: list
            fields:
              - { label: 'Name', name: 'name', widget: 'string', hint: 'First and Last' }
              - { label: 'Description', name: 'description', widget: 'markdown' }

My goal is to have each FAQ entry be able to specify an author in the editor UI by selecting one via dropdown. However, I cannot get the dropdown to display a list of authors correctly. I’ve gotten the dropdown to display two blank selections (with two authors in the YAML file)…

Another resource I looked at is https://gist.github.com/WJXHenry/a170cc1e918c5359792a802c1dbe5684, which makes me believe that trying to access a nested collection should work.

Any ideas what I’m doing wrong? Or maybe this isn’t possible?

Thanks in advance.

For now you’ll need to use have your authors in individual files within a folder collection, the relation widget doesn’t currently work against file based collections: https://github.com/netlify/netlify-cms/issues/800

1 Like