Adding Form In Block With hook_block()
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
$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
On Tue, May 20, 2008 at 2:48 PM, Steve Edwards <killshot91@comcast.net> 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.
You need to say 'return $form;' at the end of calculate_square_form(), I'm afraid. -- John Fiala
Yup, that's what it was. I was also missing return $block in hook_block(). Important stuff, I guess... Thanks. Steve John Fiala wrote:
On Tue, May 20, 2008 at 2:48 PM, Steve Edwards <killshot91@comcast.net> 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.
You need to say 'return $form;' at the end of calculate_square_form(), I'm afraid.
On Tue, May 20, 2008 at 3:15 PM, Steve Edwards <killshot91@comcast.net> wrote:
Yup, that's what it was. I was also missing return $block in hook_block(). Important stuff, I guess...
Thanks.
It's an easy mistake to make, and one I've done before. -- John Fiala
participants (3)
-
Brenda Boggs -
John Fiala -
Steve Edwards