3

I have made the following .tpl.php template:

<?php print drupal_render($element["term"]); ?>
<?php print drupal_render($element["search"]);?>
<?php print drupal_render($element["advanced_search"]);?>
<?php print drupal_render_children($element);?>

But the form tag is not generated, so the form cannot be submitted ... what am i doing wrong?

  • You are not able to get search form or any other thing ? – mohit_rocks Oct 20 '12 at 9:05
  • I see the fields but when click the search button it does not submits, because the form tag is not rendered – Cris Oct 20 '12 at 9:33
  • How does your hook_theme() function look like? – moertle Jan 29 '14 at 9:55
  • 1
    I think we need to see the $elements that you are attempting to render to build the form. – DrCord Feb 18 '15 at 18:35
6

You can add search box in a preprocess function in your template.php:

<?php
/*
 *  Preprocess page.tpl.php to inject the $search_box variable back into D7.
 */
function MYTHEME_preprocess_page(&$variables){
  $search_box = drupal_render(drupal_get_form('search_form'));
  $variables['search_box'] = $search_box;
}

Then you just need to print the variable in your page.tpl.php:

<?php print $search_box; ?>
| improve this answer | |
  • 1
    If i use drupal_render i get "Fatal error: Maximum function nesting level of '100' reached, aborting" , if i use drupal_render_children i see the form but there is not <form> tag in html so it does not work – Cris Oct 20 '12 at 13:32
  • Why do you render that form in preprocess? – Gabor Szanto Oct 20 '12 at 16:23
2
  1. You can add search box in a preprocess function in template.php of your theme:

    function MYTHEME_preprocess_page(&$variables){
          $search_box = drupal_render(drupal_get_form('search_form'));
          $variables['search_box'] = $search_box;
        }
    
  2. Then you just need to print the variable in your page.tpl.php:

    <?php print $search_box; ?>
    

You could also use module_invoke, but you shouldn't have php logic in template files:

  $block = module_invoke('search', 'block_view', 'search');
  print render($block);
| improve this answer | |
2

I wish I had a better explanation, but if you're using a custom theme hook, make sure it doesn't match your form id. For example, if your form is "mymodule_my_form," don't name your theme hook "mymodule_my_form." I've run into issues rendering a form in a template because of this.

| improve this answer | |
0

Find form_id of the form you want to display. ( you may use Get Form ID module )

Then you can put any form in any place, example:

$form = drupal_get_form('YOUR_FORM_ID');
print drupal_render($form);
| improve this answer | |
0

i had this issue, realized was printing drupal_render($form) in a fake html tag(some design time html left-over) hence no real form - removed it and there stood the real form staring at me.

| improve this answer | |
0

You just need to render complete form with individual fields.

Just change the last line of your template file with this one -

<?php print drupal_render_children($element['form']);?>
| improve this answer | |
  • 1
    ? I think your anwer is missing a line of code. – stefgosselin Jul 10 '14 at 18:14
0

This is the only solution now;

$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>';

See this https://drupal.stackexchange.com/a/111833/23593

| improve this answer | |
0
$wnid = 5; //form id
$wnode = node_load($wnid); load the node id
$form = drupal_get_form('webform_client_form_' . $wnid, $wnode, array());
print render($form);

This will give you your webform.

| improve this answer | |
0

My form was in an include file, mymodule/includes/mymodule.example_form.inc, doing the following worked for me:

function mymodule_preprocess_main_page(&$variables) {
    module_load_include('inc', 'mymodule', 'includes/mymodule.example_form');
    $example_form = drupal_render(drupal_get_form('mymodule_example_form'));
    $variables['example_form'] = $example_form;
}

Then in template file, <?php print $example_form; ?>

| improve this answer | |

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.