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

Toggle fields do not save values if they are 'toggleable' #497

Closed
rcheetah opened this issue Feb 15, 2021 · 4 comments · Fixed by #498
Closed

Toggle fields do not save values if they are 'toggleable' #497

rcheetah opened this issue Feb 15, 2021 · 4 comments · Fixed by #498
Assignees
Labels

Comments

@rcheetah
Copy link

Problem/Bug
A field of type 'toggle' that is additionally toggleable will not be saved to the frontmatter, even if a value is set. The docs allow this field to be toggleable.

Grav.toggleable.toggle.bug.mp4

Why would someone need this setting on a 'toggle' field?

The short answer: 0 does not equal NULL.

A toggle field is semantically the best way to express a boolean setting. But – controversial statement incoming – in every programming language a boolean value has not two but three states. 'true', 'false' or 'NULL'/'undefined'. A toggleable 'toggle'-field will represent all three states.

Usage example

You can add a setting for every page-type (template) to override whether or not it renders its page.title value. Let's think of two page-types, 'text' and 'gallery'. By default it makes sense, that the 'gallery' does not render its page.title, it just shows nice images. The 'text' template would, by default, render the page.title. But if the user wants one specific gallery page to render its page.title, he can override the default setting by setting the value of show_title to true. In that scenario, the developer can't use the 'default' blueprint option, because he would then set the value for the 'text' and the 'gallery' page-type equally. But with a toggleable 'toggle' field the user can choose to override the rendering of the template.

It is possible to solve that by using a three-way-switch (Yes, No, Auto), but that would clutter the frontmatter on most pages, because of an additional variable show_title: auto on every single page. Storing the value only if necessary would be cleaner, and I guess that’s kind of the purpose of toggleables.

Example blueprint will be added in a minute.

@rcheetah
Copy link
Author

@mahagr mahagr transferred this issue from getgrav/grav-plugin-admin Feb 16, 2021
@mahagr mahagr self-assigned this Feb 16, 2021
@mahagr mahagr added the bug label Feb 16, 2021
@mahagr
Copy link
Member

mahagr commented Feb 16, 2021

@rcheetah You had a bug in the field name, it has to be header.foo. Other than that the example shows a bug when saving false.

@mahagr
Copy link
Member

mahagr commented Feb 16, 2021

@rcheetah Can you please test the fix?

mahagr added a commit that referenced this issue Feb 16, 2021
@rcheetah
Copy link
Author

Oh man, sorry. What a messy test file by me. I actually run across this issue some weeks ago but didn’t have the time to file an issue, and then I mixed some things up.

I tested the PR in two environments. Works perfectly! Thank you

rhukster pushed a commit that referenced this issue Feb 16, 2021
* Toggle fields do not save `false` if they are `toggleable` [#497]

* Missing word [#497]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants