7

This seems so simple, but after reading over the docs, I couldn't find an answer:

Is there a way to have a field have a default value in Craft?

In my case, I have a Lightswitch field that I want to be on by default, so my client doesn't have to turn it on every time they publish an asset. But they still do need the ability to turn it off in exceptional cases.

I realize I could just reverse the logic of the field, but it raises a broader question of default field values (which would be useful to me).

4

As far as I know, the only fields that allow defaults are those that extend the BaseOptionsFieldType, namely Dropdown, Multiselect, Checkboxes, and RadioButtons. You could change your lightswitch to one of these other fieldtypes and have a default value available.

| improve this answer | |
2

I just submitted a feature request to have an entries.onBeforeEditEntry event. This should make it possible to have default values for all sort of field types from a plugin.

Raise an event before entry is edited/loaded in CP

| improve this answer | |
  • Apparently Brandon added a hook in response to your request. Does this now make it possible to set defaults? If so, could you perhaps expand your answer with a code sample? – Simon East Aug 5 '16 at 0:11
1

Here is a feature request to add a default value to the native Number field type – voting for it will hopefully bring this functionality to Craft CMS.

(Also, Brad Bell suggests another solution: Write a plugin that extends Craft's native Number field type.)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.