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:

// 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):

// 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

Jaypan’s picture

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

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

But I suspect you want this:

// 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?');
  }
}
Joe_Smith’s picture

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!

Jaypan’s picture

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

Joe_Smith’s picture

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

Joe_Smith’s picture

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.