1

While theming form in template file I used drupal_render to print selected fields of form. But after hit submit button, data is not saving, means submit button is not working.

I know one solution is to use drupal_render_children but it is giving me every child of form, means I'm getting all of the elements of form.

I don't want form's each and every element.

Only selected fields should get rendered.

Any way to print form selected field elements in tpl file with a working submit button?

1

One more standard way to hide form elements is to make their #access property to false , or use hide function or unset function to hide undesired elements . then you can render $form .

for example imagine you are going to hide $form['details']['name'] , in hook_form_alter you can use this snippet :

$form['details']['name']['#access'] = FALSE;

or you can hide it from being displayed , using this snippet :

hide($form['details']['name']);

then you can easily render your form in any place you desire using

print drupal_render(drupal_get_form('FORM_ID'));
2

You can render form specific form elements with the help of drupal_render. And in order to make submit button work, render all hidden form items and your submit button will work fine then.

Necessary fields for form submission are:

echo drupal_render($form['form_build_id']);
echo drupal_render($form['form_token']);
echo drupal_render($form['form_id']);

and ofcourse your submit button too.

1

Please add these four lines after render all your content type or form fields in below sequence.

 1. drupal_render($form['form_build_id']);     
 2. drupal_render($form['form_id']); 
 3. drupal_render($form['actions']);  
 4. drupal_render($form['form_token']);

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.