0

I am facing this error in node form when I going to be print title of the body field.
Error:

Notice: Undefined index: und in include() (line 4 of C:\wamp\www\drupalapi\themes\bartik\article-node-form.tpl.php).

I need to print the title of the body field, and my code looks like:

print $form['und']['#title'];

And shortly, the array is:

Array
(
[#type] => container
[#attributes] => Array
    (
        [class] => Array
            (
                [0] => field-type-text-with-summary
                [1] => field-name-body
                [2] => field-widget-text-textarea-with-summary
            )

    )

[#weight] => -4
[#tree] => 1
[#language] => und
[und] => Array
    (
        [#theme] => field_multiple_value_form
        [#field_name] => body
        [#cardinality] => 1
        [#title] => Body
        [#required] => 0
        [#description] => 
        [#prefix] => 
[#suffix] =>

        [#max_delta] => 0
        [#after_build] => Array
            (
                [0] => field_form_element_after_build
            )

        [#language] => und
        [#field_parents] => Array
            (
            )

What i am missing in my code to print the title of the body field. Thanks

  • If the field is empty it may throw this kind of error.So print this field, if it is not empty. You may get rid of this error. – Ranjani Sep 20 '13 at 6:18
  • Make sense, but i am fetching this array only on the 'Node Form' not in the node display.. – rakibtg Sep 20 '13 at 6:22
  • If you must use harcoded language selector, always replace 'und' with LANGUAGE_NONE constant when working with code. – user20301 Sep 20 '13 at 6:37
  • @user20301 is basically right, but also don't forget that some data may simply not exist in LANGUAGE_NONE and be accessible only in specified language(s), so you can never assume ['LANGUAGE_NONE'] to be set. I see he already posted good answer about how to not use it directly at all :) – Mołot Sep 20 '13 at 7:00
0

Greatly explained by user20301.
If you still not sure how to print the title then do something like this in your tpl.php file:

print $form['body']['und']['0']['#title'];

This will print the title of body field "Body" in node form.

3

To get usable markup of the field in template you need to render it before print.

print drupal_render_children($form['title']);

As explained here Theming Node Forms in Drupal 7

Additionally make sure you also render the hidden parts of the form to keep track of form identity related to system. If you wish to exlude some parts of the form, do something like this.

hide($form['field_body']);
print drupal_render_children($form);

This way drupal "render api?" can be aware of what has allready been rendered, what needs to be hidden and finally push out all required parts to make form functional.

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.