0

I am usingCCK 2 (2.9), and I know that CCK field give the option to assign the default value using the pipe, for example

Please select|Please select

But I am just curious about if exist a programatically way to assign a default value to a select list. I have several tries, using this kind of syntax:

$form['field_color']['value']['#default_value'] = 'Black'; //or
$form['field_color']['#default_value'] = 'Black'; //or
$form['field_color']['#default_value'][0]['value'] = 'Black';

but nothing. To me not is clear what is the correct syntax.

thanks in advance.

5

CCK requires a little voodoo to alter values with a hook_form_alter(). You need to implement a special type: after_build, so put this code in a custom.module:

//Declare the callback for after_build for page node form
function custom_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'page_node_form) {
    $form['#after_build'][] = 'custom_after_build';
  }
}

function custom_after_build($form, &$form_state) {
  //For a select list you have to modify this to set default to black
  $form['field_select']['value']['#value'][0] = 'black';
  //if your field is multivalued you can put more
  $form['field_select']['value']['#value'][2] = 'red';

  //for a single text it's a little different:
  $form['field_text'][0]['value']['#value'] = 'black';
  //have a look at the form after after_build
  dsm($form);
  return $form;
}

Here are some (a little confusing/complicated but useful) articles regarding this:

  • thanks for recommendations, for some reasons (some mistake in my code, sure) all sugestions are not working. – cigotete Aug 8 '11 at 17:34
  • Use the dsm($form) in the after_build function to inspect your field. – tostinni Aug 8 '11 at 18:27
  • very thanks Tostinni for your time. I will learn about how to understand the dsm($form) output! – cigotete Aug 9 '11 at 1:32
  • Regarding CCK the dsm output is quite complex, so it's trial and error, but the important part is to execute this in the after_build if not your hook_form_alter won't work. – tostinni Aug 9 '11 at 12:37
1

Make sure to enable the core PHP filter module and a PHP code textarea will show up in your content type field's configuration form, along with guidelines on how to use PHP to set a default value for your field.

In your case, for a select list, it would be something like:

  return array(0 => array('value' => 'black'));

Remember, the returned default value is supposed to be the select option's value, not label (i.e. the "black" part from the black|Black pair in the list of allowed values).

  • I think the OP ask for the hook_form_alter, your code is for the configuration of the field. – tostinni Aug 8 '11 at 16:37
  • very thanks for your suggestions. are useful for the configuration of the field. – cigotete Aug 8 '11 at 17:19
1

It looks like you were almost there with that last attempt. As Tomka pointed out, be sure you are targeting the option value, not the label.

Here's my working code sample in my custom module:

function mymodule_form_alter(&$form, &$form_state, $form_id){
    switch($form_id) {
        case 'the_form_id':
          $form['field_color']['#default_value'][0]['value'] = 'black';
          break;
    }
}
  • thank you, but I do not know why by this way the color field is deleted from the form. I will review the Tostinni answer. – cigotete Aug 8 '11 at 16:30

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.