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).

3 Answers 3

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.

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

1
  • 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
    Commented Aug 5, 2016 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.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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