1

I'm trying to programmatically set the default value (#default_value) on a textfield added via CCK. This field is in a field group. Here is my code:

/**
 * Implements hook_form_id_form_alter().
 */
function EXAMPLE_form_profile_node_form_alter(&$form, $form_state) {
  // We need to jump into a CCK field, so say we all.
  $form['#after_build'][] = 'EXAMPLE_form_profile_node_form_after_build';
}

function EXAMPLE_form_profile_node_form_after_build($form, &$form_state) {
  $form['group_company_profile']['field_pro_name']['#default_value'][0]['value'] = t('test');

  return $form;
}

I've tried various other points in the array and nothing seems to work. Both CCK and my module are weight 0. I must be missing something super obvious...?

UPDATE: One mistake was the field name. It should be field_pro_name. Updated above. It's still not working. Interestingly, I can use the after build to change the field title with:

$form['group_company_profile']['field_pro_name'][0]['value']['#title'] = 'ttt';

However, none of these changes the default value:

// In form alter:
$form['group_company_profile']['field_pro_name'][0]['#default_value']['value'] = 'ttt';
$form['group_company_profile']['field_pro_name'][0]['value']['#default_value'] = 'ttt';

// In after build:
$form['group_company_profile']['field_pro_name'][0]['value']['#default_value'] = 'ttt';
$form['group_company_profile']['field_pro_name'][0]['#default_value']['value'] = 'ttt';
2

You should be able to do something like this:

function EXAMPLE_form_alter(&$form, &$form_state, $form_id) {

  if ($form_id=='profile_node_form') {
    $form['group_company_profile']['field_pro_type'][0]['#default_value']['value'] = 'hello world';
  }

  // all other form alters here...

}

or

function EXAMPLE_form_profile_node_form_alter(&$form, &$form_state) {

  $form['group_company_profile']['field_pro_type'][0]['#default_value']['value'] = 'hello world';

}

directly from whichever form_alter() style you like.

  • Thanks for this, but it did not work. I verified the alter is firing. – Justin Aug 3 '12 at 15:17
  • Ok, it worked here so something must be different between systems :( – Jimajamma Aug 3 '12 at 15:20
  • I did just notice a cut and paste type in my answer (didn't have a closing ] after 'field_pro_type' that I just fixed :) – Jimajamma Aug 3 '12 at 15:22
  • Thanks! Re: typo, I submitted an edit for that. Guess it's not needed. Also, it works for you? Grrr! Thank you very much for trying, will dig deeper. :) – Justin Aug 3 '12 at 15:26
  • Yah, none of that array exists in the form_alter, which is (I thought) expected, since it's CCK. – Justin Aug 3 '12 at 15:29

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.