decaporg / decap-cms

A Git-based CMS for Static Site Generators
https://decapcms.org
MIT License
17.82k stars 3.04k forks source link

Feat: support hidden widget in files collections #3046

Open chokeul8r opened 4 years ago

chokeul8r commented 4 years ago

Repo: chokeul8r/gncms

I am attempting to use NetlifyCMS in a Gatsby Landing Page project for the first time. My problem relates to the config.yml file and Template key field in particular. The template key is not showing up in my frontmatter and/or my graphgl query.

backend:
  name: git-gateway
  branch: master

media_folder: static/assets
public_folder: assets

collections:
  - label: "Pages"
    name: "pages"
    files:
      - label: "Home Page"
        name: "home"
        file: "content/home.md"
        fields:
          - {
              label: "Template Key",
              name: "templateKey",
              widget: "hidden",
              default: "home-page",
            }
          - { label: Background Image, name: backgroundimage, widget: image }
          - { label: Title, name: title, widget: string }
          - { label: Sub-Title, name: subtitle, widget: string }
          - { label: Intro text, name: intro, widget: text }
          - { label: Body text, name: body, widget: text }
          - { label: Profile Image, name: profileimage, widget: image }
      - label: "Thank You Page"
        name: "thank-you"
        file: "content/thank-you.md"
        fields:
          - {
              label: "Template Key",
              name: "templateKey",
              widget: "hidden",
              default: "thank-you-page",
            }
          - { label: Title, name: title, widget: string }
          - { label: Intro text, name: intro, widget: text }
          - { label: Image, name: image, widget: image }
erezrokah commented 4 years ago

Hi @chokeul8r do you mind sharing your repo?

erezrokah commented 4 years ago

Sorry for the late reply, hidden fields are meant to be used on folder collections when creating new items: https://www.netlifycms.org/docs/widgets/hidden/#hidden since they have a pre-defined uneditable value.

erezrokah commented 4 years ago

Changed the title to reflect the underlying issue.

nettelandways commented 4 years ago

I have the exact same issue.

- { label: layout, name: layout, widget: "hidden", default: "my-template-key", }

does not work. It does not show up in the .md file that is produced through the cms

NikitaVr commented 4 years ago

The plugin gatsby-remark-source-name helped me https://www.gatsbyjs.org/packages/gatsby-remark-source-name/

It adds the name of the source so you can query it in Graphql

abdullahe commented 4 years ago

Sorry for the late reply, hidden fields are meant to be used on folder collections when creating new items: https://www.netlifycms.org/docs/widgets/hidden/#hidden since they have a pre-defined uneditable value.

Hi. Is there a workaround for this? Or an alternative solution to save "invisible" data?

erezrokah commented 4 years ago

Is there a workaround for this? Or an alternative solution to save "invisible" data?

You could use the preSave event to add any data you'd like: https://www.netlifycms.org/docs/beta-features/#registering-to-cms-events

abdullahe commented 4 years ago

Thanks @erezrokah. This method is a bit beyond my capabilities for now. :) I added a select widget and a minor inconvenience to the user.

lilpolymath commented 3 years ago

@erezrokah would it be possible to set multiple preSave events?

erezrokah commented 3 years ago

@erezrokah would it be possible to set multiple preSave events?

It should be possible to do so: https://github.com/netlify/netlify-cms/blob/b8dd038f4f693331d679bb8f39f07d9489024483/packages/netlify-cms-core/src/lib/registry.js#L223

WhiteAbeLincoln commented 3 years ago

It would be nice to have a widget that didn't display but could save a default value, regardless of whether it was in a folder or file collection, or a field under some other object widget. For my use case, I need to discriminate between different object widgets at run-time, so I'm adding a hidden "kind" field to the widget fields - but this doesn't work if I use these widgets in a file collection.

alexis-regnaud commented 3 years ago

What was the solution for that one ? I have the same problem on my side, the Collections works well, but I get a templateKey : null for my Files.

In that exemple : https://github.com/robertcoopercode/gatsby-netlify-cms/blob/master/static/admin/config.yml They use it, but seems doesn't work at all on my side :/

credli commented 2 years ago

Perhaps a "value" widget that embeds a given value but renders as read-only on the admin interface? Kind of makes more sense to me than a hidden widget with a "default" value.

- { name: templateKey, value: "blogPost", widget: value }
erezrokah commented 2 years ago

I think it makes sense to support the hidden widget (as is) for file collections. At the moment we don't even render hidden widgets:https://github.com/netlify/netlify-cms/blob/81ce05f3fb43594036d4753f8414e6cb51340376/packages/netlify-cms-core/src/components/Editor/EditorControlPane/EditorControlPane.js#L163

and default values are added during entries creation: https://github.com/netlify/netlify-cms/blob/81ce05f3fb43594036d4753f8414e6cb51340376/packages/netlify-cms-core/src/actions/entries.ts#L797

If someone is up to extend the existing code to support this, the above links should be a good place to start