0

My forms dont seem to have tags wrapping them in my templates. What am I doing wrong here?

I have a template whith some calls to theme() inside it. Two of these are forms with their own template. This template is called from a block_view().

my modules block function:

$form = drupal_get_form('stappenoverzicht_appointment_form');
  $reset = drupal_get_form('stappenoverzicht_reset_form');

  $block['content'] = theme('overzicht', array('status' => $status, 'links' =>$links, 'form' => $form, 'reset' => $reset));

my modules theme function:

$return['overzicht'] = array(
'variables' => array('status' => array(), 'links' => array(), 'form' => null, 
 'reset'      => null),
'template' => 'templates/dashboard--stappen-overzicht',
    'render element' => 'form',

);

$return['appointment'] = array(
        'arguments' => array('form' => NULL),
        'render element' => 'form',
        'template' => 'templates/form--appointment',
    );
$return['reset'] = array(
    'arguments' => array('form' => null),
    'render element' => 'form',
    'template' => 'templates/form--reset',
);
return $return;

base tpl:

<div class="appointment-container">
   <?php print theme('appointment', array('form' => $form));?>
</div>
<div class="reset-button-container">
   <?php print theme('reset', array('form' => $reset));?>
</div>

form--reset:

<div class="reset-submit-btn">
       <?php print drupal_render($form['submit']);?>
</div>
  <i><?php print drupal_render($form['description']);?></i>
<?php
print drupal_render_children($form);
?>

I came across this Render form in template answere here, but I want to manipulate the wrapping html as shown in the reset template.

  • If you don't pass the form array through drupal_get_form or relevant function that builds the form array (by adding CSRF tokens, etc), you won't see any form tags which I believe is the case here. – AyeshK Sep 13 '14 at 12:55
  • Sorry, I should have mentioned this is all called from within a block_view(). Also if I just add print render($form) in the tempklate it works as normal. I have editted my question. – Akko Sep 13 '14 at 13:14
1

your module should be like this:

function yourmodule_menu()
{
    $items = array();
    $items['path/to/form'] = array(
        'title' => 'Page Title',
        'description' => 'Page Description',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('yourmodule_form_function'),
        'type' => MENU_NORMAL_ITEM,
    );
}

function yourmodule_form_function()
{
   //build form api here as array
   //return array
}

Hope this helps you!

  • However if this is going through the theme engine it may be slightly different though the principle is the same – JimmyBorofan Sep 13 '14 at 13:03
  • Thanks Jimmy, this is indeed how I would normally do this. However my code is called within a hook_block_view. The block is then put inside a panel. – Akko Sep 13 '14 at 13:17
  • look here: drupal.stackexchange.com/questions/15152/form-within-a-block it uses drupal_render(drupal_get_form('yourmodule_form_function')) – JimmyBorofan Sep 13 '14 at 13:20
0

Just create a form

<?php
    function testform(&$form,$form_state){
    $form['description'] = array(
        '#type' => 'textfield',
        '#title' => t('Description'),
        '#default_value' => '',

      );
    return $form;

    }
    ?>

Now in menu you can use theme function

<?php
theme('test',array('myform'=>render(drupal_get_form('testform'))));
?>

And in your template file form can be used this way

<?php
print $myform;
?>

Hope it will work for you.

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.