0

I'm working on a redirect generator using 11ty and netlify-cms, and storing data in a shorturls.json file.

My config.yml looks like this:

collections:
  - name: "config"
    label: "Admin"
    description: "Administer site settings"
    create: false
    editor:
      preview: false
    files: 
      - label: short urls
        name: shorturls
        file: "_src/_data/shorturls.json"
        widget: "object"
        fields:
          - label: "Short urls"
            label_singular: "Short url"
            name: "go"
            widget: "list"
            fields: 
              - { label: "From", name: "from", widget: "string", required: false }
              - { label: "To", name: "to", widget: "string" }
              - { label: "Redirect type", name: "status", widget: "hidden", default: "301" }

My json structure looks like this:

{
  "go": [
    {
      "from": "/here",
      "to": "/there"
    },
    {
      "to": "/from-random"
    }
  ]
}

Is there a way to populate a random value to the from field? Is this going to need to be a custom widget? The json output should then look something like this:

{
  "go": [
    {
      "from": "/here",
      "to": "/there"
    },
    {
      "from": "/e6y0p13l",
      "to": "/from-random"
    }
  ]
}

Thanks

Raymond Camden
  • 10,661
  • 3
  • 34
  • 68
Matt
  • 806
  • 3
  • 12
  • 32
  • Are you saying you want to pick one FROM value in the array of values in the go object? – Raymond Camden Jan 23 '20 at 22:40
  • No, what I’m looking to do is when a user publishes a url without declaring a custom `from` value, that a random value is assigned. I guess I’m looking for a way to intercept publish generating a commit and subsequent build, preventing a blank value being assigned to `from`. I’d liken the behaviour I’m trying to create to how yourls behaves. – Matt Jan 27 '20 at 21:27
  • It sounds like you want custom logic run on the commit, not when the site is published. Right? – Raymond Camden Jan 28 '20 at 12:31
  • I wrote a custom netlifyCMS widget that does this (generating uid on entry), you can take a look in the source code maybe? https://github.com/d4rekanguok/netlify-cms-widgets/blob/master/packages/widget-id/src/control.tsx – Derek Nguyen Feb 14 '20 at 04:58

0 Answers0