2

I'm a little confused with Netlify's CMS - specifically when it comes to defining a schema via config.yml. I'm using Netlify CMS with a Gatsby static site to allow the end user to update some little bits of dynamic data on the website.

What I'm trying to achieve:

I want a data type on the CMS to allow the user to create new tour dates, with a maximum occupancy and a current occupancy (i.e. how many people are currently booked on said tour).

To achieve this I've currently gone with this config.yml (this config.yml is located in static/admin of my Gatsby project w.r.t the project root):

backend:
  name: git-gateway
  branch: master

publish_mode: editorial_workflow

media_folder: src/images/uploads
public_folder: /uploads

collections:
  - name: "tourInfo"
    label: "Tour Info"
    description: "Upcoming tours and their availability"
    files:
      - label: "tour"
        name: "Tour"
        file: "static/tours.json"
        fields:
          - {label: "Tour Date", name: "date", widget: "datetime"}
          - {label: "Total Places", name: "totalPlaces", widget: "number"}
          - {label: "Filled Places", name: "filledPlaces", widget: "number"}

This doesn't seem to be what I'm looking for though, I can't seem to be able to create numerous instances of tour dates, but only one. Perhaps this is a fundamental misunderstanding of mine when it comes to collection types, but I thought I could define tour as a single file then populate it with numerous instances of tour.

Can someone point me in the right direction of how to achieve this array of tour data points without death by file?

BML91
  • 2,952
  • 3
  • 32
  • 54

1 Answers1

5

You can either make that collection a folder collection or use a list widget.

An example of using list, with your code:

collections:
  - name: "tourInfo"
    label: "Tour Info"
    description: "Upcoming tours and their availability"
    files:
      - label: "tour"
        name: "Tour"
        file: "static/tours.json"
        fields:
          - label: "Tour List"
            name: "tourList"
            widget: "list"
            fields:
              - {label: "Tour Date", name: "date", widget: "datetime"}
              - {label: "Total Places", name: "totalPlaces", widget: "number"}
              - {label: "Filled Places", name: "filledPlaces", widget: "number"}

So your tour date, place, etc. can be placed under a list's fields. It will look really unruly for a large amount of data though, you might want to use a custom widet to paginate it, or use folder collection.

Derek Nguyen
  • 11,294
  • 1
  • 40
  • 64
  • Thanks, for the sake of total clarity, where in that example would you add the `file`(`s`) variable? – BML91 Apr 05 '19 at 09:08
  • Just tried it myself, and I can't seem to figure out how to have the `list` data under a single file. I'd be grateful if you could update to show how. – BML91 Apr 05 '19 at 13:16
  • @BML91 sure, I've updated the example to use yours. Let me know if it still doesn't work for you – Derek Nguyen Apr 06 '19 at 08:41
  • This config product a json object like: `{tourList: [{date: ...}]}` not an array. Is there a way to product directly an array? – ManUtopiK Feb 04 '21 at 20:52
  • 1
    @ManUtopiK I don't think that's possible at the moment, the json produced by netlifyCMS will always be wrapped in an object – Derek Nguyen Feb 05 '21 at 03:08
  • 1
    @DerekNguyen Thx for your answer. That the conclusion I did after reading this https://github.com/netlify/netlify-cms/issues/531 – ManUtopiK Feb 05 '21 at 16:37