Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete front matter values completely if optional string widget left empty in CMS #995

Open
OrdinaryJellyfish opened this issue Jan 8, 2018 · 10 comments

Comments

@OrdinaryJellyfish
Copy link

OrdinaryJellyfish commented Jan 8, 2018

- Do you want to request a feature or report a bug?
Request a feature

- What is the current behavior?
When emptying a field in the CMS, then re-publishing, it leaves the example: there

- If the current behavior is a bug, please provide the steps to reproduce.

- What is the expected behavior?
Delete it completely

- Please mention your CMS, node.js, and operating system version.

Netlify CMS 1.0, Node.js 6.12.3

- Please link or paste your config.yml below if applicable.

backend:
  name: git-gateway
  branch: master # Branch to update (optional; defaults to master)
  
publish_mode: editorial_workflow
media_folder: "images"

collections:
  - name: "scripts" # Used in routes, e.g., /admin/collections/blog
    label: "Script Page" # Used in the UI
    folder: "_posts" # The path to the folder where the documents are stored
    create: true # Allow users to create new documents in this collection
    slug: "{{year}}-{{month}}-{{day}}-{{slug}}" # Filename template, e.g., YYYY-MM-DD-title.md
    fields: # The fields for each document, usually in front matter
      - {label: "Layout", name: "layout", widget: "hidden", default: "script"}
      - {label: "Script Name", name: "title", widget: "string"}
      - {label: "GitHub Username", name: "gh_username", widget: "string"}
      - {label: "Repository Name", name: "repo_name", widget: "string"}
      - {label: "Screenshot", name: "screenshot", widget: "image", required: false}
      - {label: "Documentation URL", name: "doc_url", widget: "string", required: false}
      - label: "Category"
        name: "categories"
        widget: "select"
        options:
         - { label: "Blog", value: "Blog" }
         - { label: "Documentation", value: "Documentation" }
      - label: "Does this repository use Netlify CMS?"
        name: "cms"
        widget: "select"
        options:
         - { label: "No", value: "false" }
         - { label: "Yes", value: "true" }
      - {label: "Description", name: "body", widget: "markdown"}
@tech4him1
Copy link
Contributor

@TristianK3604 What field were you wondering this about? Most fields seem to be removed automatically for me.

@OrdinaryJellyfish
Copy link
Author

@tech4him1 It's for all the front matter fields, the one I specifically would like Netlify CMS to remove is "Documentation URL".

@tech4him1
Copy link
Contributor

It looks like this is only for string widget fields, other fields are removed from a file if empty and optional.

@tech4him1 tech4him1 changed the title Delete front matter values completely if field left empty in CMS Delete front matter values completely if optional string widget left empty in CMS Jan 10, 2018
@erquhart
Copy link
Contributor

Unfortunately, fixing this would represent a breaking change, since templates may be relying on an ever-present field with an empty string when empty.

@TristianK3604 honestly, I'm tempted to say removing vs. not removing is a horse a piece. The inconsistency isn't good if other widget fields are removed when empty, true, but can't you just check for the empty string value for your use case?

@stale
Copy link

stale bot commented Oct 29, 2019

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.

@stale stale bot added the status: stale label Oct 29, 2019
@erquhart
Copy link
Contributor

erquhart commented Dec 17, 2019

This issue best explains #2848, #2007, #1449, and #2017 - closing those in favor of this.

When a widget returns a falsey value that should not be persisted (often an empty string), the widget should instead pass null to onChange, which results in the key being removed in the output. We can't enforce this from the core because the Widget API should support persisting an empty string if that's what's desired, but the in-repo widgets should be doing this.

Because many Gatsby sites are likely depending on the current behavior, we should introduce this as off-by-default until 3.0, but enough users have brought up this issue that it seems worth addressing sooner rather than later.

@pmpinto
Copy link

pmpinto commented Oct 28, 2020

Here's another use case and why this is relevant for me:
When you have an editable slug field that will be used by Jekyll in a permalink like /blog/{{slug}}/.

By default Jekyll will get the slug from the file name, which comes from the title.

title: 'Oh hai there' # URL would be: /blog/oh-hai-there/

But that can be overwritten with the slug property.

title: 'Oh hai there'
slug: 'hai' # URL is now /blog/hai/

What's sad is that if you change your mind and remove the value in slug through the CMS after creating the file, the slug property will still be in the frontmatter as an empty string—which will effectively ruin the URL.

title: 'Oh hai there'
slug: '' # URL is now ruined /blog//

@oliverlambson
Copy link

In case anyone's looking for how to deal with this for frontmatter images Gatsby.

You can work around this for now by explicitly typing the field in your GraphQL schema with File @fileByRelativePath

In gatsby-node.js:

exports.createSchemaCustomization = ({ actions }) => {
  const { createTypes } = actions

  // Explicitly define the Markdown frontmatter object
  // This way those will always be defined as file even if CMS leaves a blank string
  // This way the queries will return `null` even when a blank string is left
  createTypes(`
    { ... }

    type MarkdownRemark implements Node {
      frontmatter: Frontmatter
      fields: Fields
    }

    type Frontmatter {
      { ... }
      imagePath: File @fileByRelativePath // this is the important bit
      { ... }
    }

    { ... }
  `)
}

@dinoboff
Copy link

It also affect the image widget. Removing an image will leave an empty string.

@danielmoessner
Copy link
Contributor

danielmoessner commented May 19, 2021

@oliverlambson

imagePath: File @fileByRelativePath

I've looked all over the place until I found your solution. Your solution is really elegant, thanks for sharing. I'm just wondering how did you find out that File @fileByRelativePath works? Or that the File Type exists? Is it in the Gatsby Documentation somehwere?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants