I have made a custom content type called "car". It has a text field called "field_color". I have made a custom module called "car_form".

This works:

<?php
// adds the text "Welcome to the form" to the top of the form
function car_form_form_alter(&$form, &$form_state, $form_id) {
  if (
$form_id == 'car_node_form') {
    
$form['welcome'] = array(
     
'#markup' => '<h3 class="form-section-title">Welcome to the form</h3>',
    );
  }
}
?>

This does not work (nothing happens):

<?php
// This code intends to change the label for the "color" field
function car_form_form_alter(&$form, &$form_state, $form_id) {
  if (
$form_id == 'car_node_form') {
   
$form['field_color']['und']['#title'] = t('What color is your car?');
  }
}
?>

Seems like a simple issue. Can anyone help?

Comments

You are targeting the incorrect location. You can do a dump of the form using:

<?php
drupal_set_mesage
('<pre>' . print_r($form['field_color'], TRUE) . '</pre>');
?>

But I suspect you want this:

<?php
// This code intends to change the label for the "color" field
function car_form_form_alter(&$form, &$form_state, $form_id) {
  if (
$form_id == 'car_node_form') {
   
$form['field_color'][LANGUAGE_NONE][0]['#title'] = t('What color is your car?');
  }
}
?>

Jaypan
Our newest Drupal site: PacificAikido.com (Drupal showcase)

Thanks for your reply. The location $form['field_color'][LANGUAGE_NONE][0]['#title'] does not appear in the form dump. "Title" appears 5 times in the dump you suggested:

1. $form['und']['0']['#entity']->title
2. $form['und']['0']['#title']
3. $form['und']['0']['value']['#entity']->title
4. $form['und']['0']['value']['#title']
5. $form['und']['#title']

None of these work. I do wonder what is stopping hook_form_alter!

LANGUAGE_NONE is equal to 'und', and in fact should be used instead of 'und'.

As for your elements, none of them are correct, as they have all missed the element name 'field_color'.

Jaypan
Our newest Drupal site: PacificAikido.com (Drupal showcase)

Thanks, that was a typo by me in the posting above. $form['field_color'][LANGUAGE_NONE][0]['#title'] does not work though.

This works: $form['field_color'][LANGUAGE_NONE][0]['value']['#title']= t('What color is your car?');.

Thanks for your help Jaypan - I appreciate your quick replies.