User login

Set a default value for a CCK checkbox

Resolution: this is built into the field configuration GUI (graphical user interface)! Right under "Label." No need for anything fancy. I am a failure as a Drupal expert.

no working answer:

Default value for CCK Check box field
http://drupal.org/node/200486

Example for allowed values php code?
http://drupal.org/node/125464

Labels for single on/off checkbox field aren't appearing
http://drupal.org/node/115026

Advanced Usage Only: PHP code that returns a keyed array of allowed values. Should not include <?php ?> delimiters. If this field is filled out, the array returned by this code will override the allowed values list above.

return array(0 => array('Default value' => 1));

Does not work, it overrides the Allowed values list above:

0|Receive CDM media alerts by email
1|Receive CDM media alerts by email

So let's build this thing completely:

return array(
  0 => array(
    'value' => 'Receive CDM media alerts by email',
  ),
  1 => array(
    'value' => 'Receive CDM media alerts by email',
    'Default value' => TRUE,
  )
);

Result:

[ ] Array

Rats.

Attempt two, just a test:

return array(
  0 => 'Receive CDM media alerts by email',
  ),
  1 => 'Receive CDM media alerts by email',
);

[ ] Receive CDM media alerts by email

But if I delete

0|Receive CDM media alerts by email
1|Receive CDM media alerts by email

-- even though the returned array is supposed to override, it all disappears!

So it ain't working:

return array(
  0 => 'Receive CDM media alerts by email',
  ),
  1 => 'Receive CDM media alerts by email',
  'Default value' => 1,
);

Ugh!!! Right under "Label" there's a collapsed form field for Default value. Doh!

Resolution

Under the form field:

Label: *

Click the collapsed form field- AFTER you have already submitted the configuration form once, so that the possible values are saved:

Default value

Expanding that, in Agaric's case we see this:

[ ] Receive CDM media alerts by email

Let's just check that off...

Done.

No PHP code needed anywhere.

Off to get my vision checked...

Searched words: 
CCK checkbox field default Drupal CCK text checkbox PHP advanced

Comments

Failures of the world, Unite!

I am a failure as a Drupal expert.

LOFL (LOF**'nL)
This was so great I was willing to create an account just to say something.

I've got 2 FT years working in drupal and consider myself if not an expert at least familiar with the territory.

Well, I just spent 45 minutes trying to set a checkbox default (and trying to figure out why two in the same content type were defaulting one way, and a third was defaulting the other). Tried messing with the allowed values list, also toggling the "Required" setting.

Finally, starting to think of maybe a form_alter hack, I did a quick Google and this post came up on top.

The great part for me is that I'm the one who set them in the first place, lo about 2 months ago!

Thanks for the help and the amusement!

Wow

Wow, the documentation on this is beyond useless. You've saved the day. Thanks :)

Working, but inconsistently

I was having this problem with a Drupal 5 install. I upgraded to 5.x-1.7 (from 1.6.1) and found that there is still some inconsistency in how default values are managed in my account. I can shift them from being enabled to disabled and back, however it seems to be dependent on there being Allowed values list defined (or not in some tests). I had assumed that if you created a checkbox as an integer it would take the label and insert a 1 or 0. In anycase, trying variations for:

Either as an integer checkbox:
0
1|Checkbox title

or text checkbox:
0
Checkbox title

Has seemed to work for the moment.

Mike

Cheers mate. Found my way

Cheers mate. Found my way here googling for "default value cck". Gotta love how smart you feel, not only by not noticing the "set default value" option but by having to google your way to it. Anyways. Cheers for helping out ;)

huh?

A lot more attention was paid to describing the problem here than the solution, and while the post seemed helpful to some
What do you mean 'under the form field label:*
Expanding what?
What is the meaning of the square brackets?

yes, BUT! ...

If you have a field where the checkbox select (etc) will not come up, we still haven't answered the question.

thanks

0
1|Checkbox title

^^^
perfect.

Not working. Setting the

Not working.

Setting the default to checked doesn't work.

Awesome! Oh wait, too bad

Awesome! Oh wait, too bad that you cannot use PHP snippets in Drupal 7. What genius decided to leave it out?

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.