Drupal 7 - Form in a Block With Template File

Submitted by nigel on Wednesday 24th October 2012

I have been coming up against the same Drupal 7 requirement over and over recently - the need to have a form in a block with a separate template file I can pass over to the front-end guy to weave his magic. This is one of my easiest ever tutorials, and is really only here as an aide memoire for when I can't quite remember the exact syntax in say hook_theme or I can't remember which rendering functions I call in the template.

By using the module below, you have an excellent boilerplate to start your FAPI coding since this pattern repeats (as I have noted) over and over! Too easy!

fb_pattern.info
name = Form / Block Pattern
description = Boilerplate form in a block pattern code with template file
core = 7.x
fb_pattern.module
<?php

/*
 * File         : fb_pattern.module
 * Title        : Drupal 7 Boilerplate form in a block pattern with template file
 * Author       : www.badzilla.co.uk @badzillacouk
 *
 * This work is copyright BadZilla under the GPL licence terms and conditions
 *
 */


/*
 * Implements fb_pattern_info
 */
function fb_pattern_block_info() {
    
    
$block = array();

    
// Sign up block
    
$block['fb_pattern'] = array(
                
'info' =>   t('Form / Block Pattern Example'),
                
'weight' => 0);  

    return 
$block;
}



/*
 * Implements hook_block_view
 */
function fb_pattern_block_view($delta) {
    
    
$block = array();

    switch(
$delta) {
        case 
'fb_pattern':
            
$block['title'] = t('Sample Form in a Block Pattern');
            
$block['content'] = drupal_get_form('fb_pattern_form'); 
            break;
    }

    return 
$block;
}




function 
fb_pattern_form($form, &$form_state) {
    
    
$form['age'] = array(
        
'#title' => t('Age'),
        
'#type' => 'textfield',
        
'#required' => TRUE,
        
'#maxlength' => 3,
        
'#size' => 3,
        
'#weight' => 0,
    );

    
$form['submit'] = array(
        
'#type' => 'submit',
        
'#value' => t('Submit'),
        
'#weight' => 10,
        
'#validate' => array('fb_pattern_form_validate'),
        
'#submit' => array('fb_pattern_form_submit'),
    );

    
$form['#theme'][] = 'fb_pattern_form';
   
    return 
$form;
}



function 
fb_pattern_form_validate($form, &$form_state) {
    
    if (!
is_numeric($form_state['values']['age']))
        
form_set_error('age't('That doesn\'t look like a valid age to me!'));

}



function 
fb_pattern_form_submit($form, &$form_state) {
    
    
drupal_set_message(t('Wow! You don\'t look :age - have you had work done?', array(':age' => $form_state['values']['age']))); 
}



/*
 * Implements hook_theme
 */
function fb_pattern_theme() {
    
    return array(
        
'fb_pattern_form' => array(
            
'arguments' => array('form' => NULL),
            
'render element' => 'form',
            
'template' => 'fb_pattern_form',
        ),
    );
}
?>
fb_pattern_form.tpl.php
<?php
/*
 * File         : fb_pattern_form.tpl.php
 * Title        : Drupal 7 Boilerplate form in a block pattern with template file
 * Author       : www.badzilla.co.uk @badzillacouk
 *
 * This work is copyright BadZilla under the GPL licence terms and conditions
 *
 */


?>
        

    <?php print render($form['age']); ?>


    <?php print render($form['submit']); ?>


    <?php print drupal_render_children($form); ?>
blog terms
Drupal Drupal 7