1

I have a cutom module which creates a page with a custom form. the problem is that i don't print whole form at once like :

$form = drupal_get_form('custom_example_form', $adverts);
print render($form);

Rather my code is like:

$form = drupal_get_form('custom_example_form', $adverts);
print render($form['name']);
print render($form['descriere']);
print render($form['submit']);
print render($form['form_id']);
print render($form['form_build_id']);

The problem us that is second example drupal is not generating

<form id="" accept-charset="" method="" action="">

How do i generate in second situation?

2
  • 1
    You should not mix print statements (for HTML markup) and drupal_get_form() in the same file. Print statements (for HTML markup) belongs to template files. A template should not call drupal_get_form() (because getting a form is not the job of a template). Commented Apr 24, 2014 at 19:16
  • you're right, now i realized my mistake but is too late to get back, I ended using an alternative, non-elegant solution however. Commented Apr 25, 2014 at 7:42

4 Answers 4

2

You should not print individual children elements of the form array, but render it completely (with render($form)).

If you want to fine tune the markup inside the <form> tag, you need to alter the form (using hook_form_FORM_ID_alter()) or provide a custom theme used for the form (i.e. in hook_theme() returns an items with the form ID as key).

1

I ended using this method to solve my issue, not an elegant solutions but it works:

in tpl:

$form = drupal_get_form('custom_example_form', $adverts);
print '<form id="'.$form['#id'].'" accept-charset="UTF-8" method="'.$form['#method'].'" action="'.$form['#action'].'">'; 
print render($form['name']);
print render($form['descriere']);
print render($form['submit']);
print render($form['form_id']);
print render($form['form_build_id']);
print '</form>';

it doesn't look good but is working.

1

If you are selectively rendering the form, you could unset some fields and render the whole form.

2
  • this could be a solution at least is works Commented Apr 25, 2014 at 7:44
  • 1
    Rather than unsetting some array indexes, it is better to change the #access property for those form fields that needs to be hidden.
    – avpaderno
    Commented May 1, 2014 at 16:50
0

Try This,

$form = drupal_get_form('custom_example_form');
print render($form);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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