[development] Adding Form In Block With hook_block()

Brenda Boggs brenda at alligatorsneeze.com
Tue May 20 21:06:40 UTC 2008


$block['content'] = drupal_get_form('calculate_square_form');

and make sure to return $block

Steve Edwards wrote:
> I didn't get any bites on this one on the support list, so I'm hoping 
> some of you development gurus can help me.
>
> I would like to add a simple area calculator form (length x width x 
> depth = area) to a sidebar block, and I'm having problems figuring how 
> to get the form to show up in the block using hook_block().  I've 
> gotten the block to show up in the blocks list and have enabled it, 
> but I can't get it to show up.  I've been using the blocks chapter in 
> the PDD book as a guideline, but I must be missing something.
>
> Here's what I have so far.
>
> <?php
>
> function calculate_square_form() {
>     $form['length'] = array(
>         '#title' => t('Length'),
>         '#type' => 'textfield',
>         );
>        
>     $form['width'] = array(
>         '#title' => t('Width'),
>         '#type' => 'textfield',
>         );       
>
>     $form['depth'] = array(
>         '#title' => t('Depth'),
>         '#type' => 'textfield',
>         );
>        
>     $form['depth'] = array(
>         '#title' => t('Area'),
>         '#type' => 'textfield',
>         );
>
>     $form['length'] = array(
>         '#type' => 'button',
>         '#value' => t('Calculate area')
>         );
> }       
>
> function shbark_calculate_block($op = 'list', $delta = 0, $edit = 
> array()) {
>     switch ($op) {
>         case 'list':
>             $blocks[0]['info'] = t('Square area calculator');
>             return $blocks;
>            
>         case 'view':
>             $block['subject'] = t('Area calcultor');
>             drupal_get_form('calculate_square_form');
>             break;
>     }
> }
>     
> How do I get my block content to be displayed?  Do I have to pass the 
> form to a theme function?  Do I assign the form data to 
> $block['content'] somehow?  Or do I have to put the form array in the 
> 'view' case for $op instead of a separate form function?
>
> Thanks.
>
> Steve


More information about the development mailing list