Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

Here's an example I just ran into. I'm trying to put some HTML in the label of my field.

$form['field_feedback']['und']['#title'] <- has the title
$form['field_feedback']['und'][0]['#title'] <- has the title
$form['field_feedback']['und'][0]['value']['#title'] <- has the title. (Works)

Why are there three populated #title attributes with only one being used?

share|improve this question
1  
where this $form array is generated? as I know we have no $form with this structure in hook_form_alter –  Mohammad Ali Akbari Apr 7 '12 at 8:14

1 Answer 1

up vote 45 down vote accepted

What you're seeing is how Drupal represents fields in a form. It breaks down like this:

  • The children of the outer array, $form['field_back']['und'] ('und' for 'undefined'), are the different languages the field has been translated into
  • The children inside each language array, e.g. $form['field_feedback']['und'][0], represent the different entries for the field. For example, if a field has unlimited cardinality, and there are several entries for that field, they will show as $form['field_feedback']['und'][0], $form['field_feedback']['und'][1], etc.
  • Finally you have the form input element(s), for the field values themselves, e.g. $form['field_feedback']['und'][0]['value']. In this case the column name is value, for a node reference field type it would be nid, and so on. If a field has multiple data columns associated with it you'll see more children as well. These are represented by a text field/select box/special widget, and will ultimately provide the value for the field.

The fact that titles have been added to the various elements throughout the hierarchy is incidental to be honest. Whether or not those titles are displayed on the form is down to the particular settings of the render array in which each #title is contained.

To change the title on the actual form widget, you only need to worry about the form element at the very bottom of the array: i.e. the actual input elements ($form['field_feedback']['und'][0]['value'], etc.).

It's probably worth noting that the element/widget used for a particular field may be quite complex. If you don't already have it, you should install the Devel module and use the dpm() function to inspect the $form array; that will show you exactly where in the array the elements you're trying to target are.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.