Any text field that happens to be converted to a container type has issues with hook_form_alter and changing the title of said field.

Traditional methods have undesired effects. For instance, this will remove my field from the display, performing an overwrite instead of an override.

$form['my_field'] = array(
  '#title' => t('New Title'),
);

This is a print out using print_r($form) for my field:

[my_field] => Array
  (
    [#title] => New Title
    [#tree] => 
    [#parents] => Array
      (
        [0] => my_field
      )

    [#array_parents] => Array
      (
        [0] => my_field
      )

    [#weight] => 0.033
    [#processed] => 
    [#required] => 
    [#attributes] => Array
      (
      )

    [#title_display] => before
    [#id] => edit-my-field
    [#sorted] => 1
    [#children] => 
    [#printed] => 1
  )

The below code has absolutely no effect on the rendering at all.

$form['my_field']['#title'] = t('New Title');

Fortunately, I found a way that works after trying every single mention of ['#title'] under the my_field printout.

$form['my_field']['und'][0]['value']['#title'] = t('New Title');

It would be much more efficient to override the array (like the first example) or use the top-level ['#title'] to change the title of a container element.

Comments

JuliaKoelsch’s picture

I also ran into this issue. I was trying to change the #required value of textfields in a hook_form_alter. Doing this for textfields had no effect:

$form['my_field']['#required'] = TRUE;

However, using the above code for select lists DID work.

I modified my code as @jhipp suggested, and was able to change the #required value for my textfields as well.

I tried walking through the code to understand why the two fields were treated differently, but didn't find it.

Version: 7.0 » 7.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.