hello!

i'm developing custom module, with quite large entity with lots of fields. i'm having trouble getting template preprocessing to work as expected.

the problem is that when ever i try to do any preprocessing on the form elements, the <form> tag doesn't get printed.

for example, this works as expected:

  in preprocess -function:
  $variables['rest'] = drupal_render($variables['form']);

  and in the template file:
  print($rest);

but if try and extract form elements off from the form variable:

preprocess:
$variables['customer_name']=drupal_render($variables['form']['customer_name']);  
$variables['rest'] = drupal_render_children($variables['form']);

template:
print($customer_name);
print($rest);

the form prints as expected, apart from the <form> tags.

i have the whole form wrapped like so, because of the ajax updates.

$form['#prefix'] = '<div id="order-form-wrap">';
$form['#suffix'] = '</div>';

and the theme is declared like so in module_theme() function:

'order_add' => array(
      'render element' => 'form',
      'template' => 'orders-form', // TemplateFileName.tpl.php
      'path' => drupal_get_path('module', 'module_name'),
    ),

suppose that i'm missing something fundamental here, but just can't figure out what it is.

so... how to get those missing form tags printed? i'm pretty much stuck here, so any help would be appreciated!

cheers,

janne

Comments

jiisuominen’s picture

i started thinking that this is something that i've don incorrectly here and while that still is possibility, atleast i'm not alone aith this one: http://drupal.stackexchange.com/questions/5468/how-to-render-the-form-tag

that link illustrates my problem exactly.

anyone?

Jaypan’s picture

If you are rendering form elements, you should be doing this in a theme function, not a preprocess function. Preprocess functions are for generating data that will be used in the form (if it's a form preprocess), not for generating the form itself.

funkju’s picture

EDIT: I guess that wasn't your exact problem. But I found the thread searching for "missing form tags". So hopefully this will help someone.

I was having the same trouble. My code looked like this:

function module_name_form(){
               $form = array();

		$form['client_name'] = array(
			'#name' => 'client_name',
			'#type' => 'textfield',
			'#title' => 'Client Name',
			'#required' => TRUE
		);

                .....

                return $form;
}

But a sentence in the hook_menu() documentation showed the problem

Special care should be taken for the page callback drupal_get_form(), because your specific form callback function will always receive $form and &$form_state as the first function arguments

Which is a fancy way of saying that I need to pass in those parameters.

function module_name_form($form, &$form_state){
		$form['client_name'] = array(
			'#name' => 'client_name',
			'#type' => 'textfield',
			'#title' => 'Client Name',
			'#required' => TRUE
		);

                .....

                return $form;
}

Cleared Drupal's cache and the problem was resolved.

Jaypan’s picture

// Incorrect so removed.