Netlify-cms: Implement Placeholders for Widget Defaults and add UUID Placeholder

Created on 27 Dec 2018  路  8Comments  路  Source: netlify/netlify-cms

Is your feature request related to a problem? Please describe.
This is an issue to flesh out the discussion in #1407. It aims to describe how the discussed solution would be implemented. The goals are:

  1. Generalise the use of placeholders so that they can be used in places other than in generating the slug field.
  2. Add a uuid placeholder that is replaced by a UUID.
  3. Enable the use of placeholders within widgets' default field.

Proposed Solution

  1. Extract token replacement / placeholders to separate until that allows a configurable map of tokens to be supplied.
  • Extract functionality in slugformatter to a formatter utility to make it generic. Allow a map of tokens => functions to be passed in to customise the behaviour. This way the supported tokens can be varied based on where the formatter is used.
  • Configure a formatter to replicate current slug formatter functionality.
    Note: At this point, functionality should be identical to present.
  1. Add uuid field to the placeholder tokens supported by the slug formatter.
  • Create a function for generating a uuid that wraps [uuid lib]https://www.npmjs.com/package/uuid), using uuidv1().
  • This will allow a uuid to be used inside slugs which I think is worthwhile.
  1. Add formatting to widget's default value resolution.
  • Create a new Formatter for use in processing a widget's default field.
  • Currently there is a check for a default value. If a default value exists it should be run through the formatter and the returned value set as the defaultValue.

There are some further questions, but I don't think they necessarily need to be resolved now and could be added later to keep this feature small and focussed:

  • Could user-defined placeholder functions be supported?
  • Should a UUID field be added by default to all collection items?
  • Could this be opt-in and enabled by a setting on the collection?
  • Should there be a mechanism to add a UUID to existing collection items if one doesn't exist?
  • Should relations default to using a uuid prop as the value for valueField?

Note: I have a little time over the next week and should be able to get this done if the above is acceptable.

Describe alternatives you've considered
There is the alternative described in #1407 which involves creating a widget to dynamically supply a UID. However I think a more low-level solution is preferable. Relations should not be being declared based on properties that can be edited by a user. This solution allows a stable field to be created at creation time and set as a hidden widget to prevent user-editing.

Most helpful comment

Tom mentioned this issue in gitter chat & I'd like to share my custom id widget here for those who needs to solve this issue right now: https://github.com/d4rekanguok/netlify-cms-widgets

Please note that this is an implementation of an alternative that @Undistraction has already mentioned above:

There is the alternative described in #1407 which involves creating a widget to dynamically supply a UID. However I think a more low-level solution is preferable. Relations should not be being declared based on properties that can be edited by a user. This solution allows a stable field to be created at creation time and set as a hidden widget to prevent user-editing.

A bit lack of doc (sorry), but the usage for the id widget is fairly simple. Here's an example:

In cms.js

// Register the widget
import cms from 'netlify-cms-app'
import { Widget as IdWidget } from '@ncwidgets/id'

cms.registerWidget(IdWidget)
cms.init()

In config.yml

collections:
  - label: "Posts"
    name: "posts"
    folder: "_posts"
    create: true
    fields:
      - label: ID
        name: id
        widget: ncw-id
        prefix: post        # will generate post-124hfkjas
        timestamp: true     # will generate 1245119112-post-124hfkjas
        hint: This widget generate an unique read-only id

You can view a demo at https://custom-widgets.netlify.com/

Feedback & contribution is much, much appreciated.

All 8 comments

@Undistraction what do you think about this as a potential solution: https://github.com/netlify/netlify-cms/issues/1409#issuecomment-458781037

Any progress on this feature? It's really needed for relations. CMS use cases like predefined taxonomies, separate author bios to be linked from posts, series collections with previous/last all need a relationship identifier that can survive editing the relationship target.

Hey @dopry - totally agree on priority, but no one has dug into it yet.

Tom mentioned this issue in gitter chat & I'd like to share my custom id widget here for those who needs to solve this issue right now: https://github.com/d4rekanguok/netlify-cms-widgets

Please note that this is an implementation of an alternative that @Undistraction has already mentioned above:

There is the alternative described in #1407 which involves creating a widget to dynamically supply a UID. However I think a more low-level solution is preferable. Relations should not be being declared based on properties that can be edited by a user. This solution allows a stable field to be created at creation time and set as a hidden widget to prevent user-editing.

A bit lack of doc (sorry), but the usage for the id widget is fairly simple. Here's an example:

In cms.js

// Register the widget
import cms from 'netlify-cms-app'
import { Widget as IdWidget } from '@ncwidgets/id'

cms.registerWidget(IdWidget)
cms.init()

In config.yml

collections:
  - label: "Posts"
    name: "posts"
    folder: "_posts"
    create: true
    fields:
      - label: ID
        name: id
        widget: ncw-id
        prefix: post        # will generate post-124hfkjas
        timestamp: true     # will generate 1245119112-post-124hfkjas
        hint: This widget generate an unique read-only id

You can view a demo at https://custom-widgets.netlify.com/

Feedback & contribution is much, much appreciated.

Thanks so much for sharing @d4rekanguok!

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Not stale

This should really be integrated in the core netlify cms. I had a lot of headaches with relations...

Was this page helpful?
0 / 5 - 0 ratings