Thursday, March 10, 2011

Drupal 7 hook_theme not using the template

Do check out this if you want to use the template .tpl.php


http://harshal-techapps.blogspot.com/2011/03/drupal-7-hooktheme-template-form.html

<?php

function deductible_menu()
{
    $items['deductible/calculate'] = array
    (
        'title' => 'Theming Forms',
        'description' => 'Practicing theming forms in Drupal 7',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('deductible_form'),
        'access callback' => TRUE,
    );
    return $items;
}

function deductible_form(){
$form['first_name'] = array
(
    '#type' => 'textfield',
);
$form['last_name'] = array
(
    '#type' => 'textfield',
);
$form['age'] = array
(
    '#type' => 'textfield',
    '#maxlength' => 3,
);

// Get the path to the module
$path = drupal_get_path('module', 'Deductible');
// Attach the CSS and JS to the form
/*$form['#attached'] = array
(
    'css' => array
    (
        'type' => 'file',
        'data' => $path . '/form_theme.css',
    ),
    'js' => array
    (
        'type' => 'file',
        'data' => $path . '/form_theme.js',
    ),
);*/
return $form;
}


function deductible_theme()
{
    return array
    (
        'deductible_form' => array
        (
            'render element' => 'form'
        ),
    );
}


function theme_deductible_form($variables)
{
    // Isolate the form definition form the $variables array
    $form = $variables['form'];
    $output = '<h2>' . t('Please enter your information below') . '</h2>';
    // Put the entire structure into a div that can be used for
    // CSS purposes
    $output .= '<div id="personal_details">';
    // Each of the pieces of text is wrapped in a <span>
    // tag to allow it to be floated left
    $output .= '<span>' . t('My name is') . '</span>';
    // Form elements are rendered with drupal_render()
    $output .= drupal_render($form['first_name']);
    $output .= drupal_render($form['last_name']);
    $output .= '<span>' . t('and I am') . '</span>';
    $output .= drupal_render($form['age']);
    $output .= '<span>' . t('years old.') . '</span>';
    $output .= '</div>';
    // Pass the remaining form elements through drupal_render_children()
    $output .= drupal_render_children($form);
    // return the output
    return $output;
}

Drupal 7 hook_theme template form

Drupal 7 hook_theme template form

First create a module using the link http://harshal-techapps.blogspot.com/2011/03/database-drupal-7-themetable-example.html

deductible.module

<?php

function deductible_menu()
{
    $items['deductible/calculate'] = array
    (
        'title' => 'Theming Forms',
        'description' => 'Practicing theming forms in Drupal 7',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('deductible_form'),
        'access callback' => TRUE,
    );
    return $items;
}


function deductible_form(){
$form['first_name'] = array
(
'#title' => 'FirstName',   
'#type' => 'textfield',
);
$form['last_name'] = array
(
    '#type' => 'textfield',
);
$form['age'] = array
(
    '#type' => 'textfield',
    '#maxlength' => 3,
);

// Get the path to the module
$path = drupal_get_path('module', 'Deductible');
// Attach the CSS and JS to the form
/*$form['#attached'] = array
(
    'css' => array
    (
        'type' => 'file',
        'data' => $path . '/form_theme.css',
    ),
    'js' => array
    (
        'type' => 'file',
        'data' => $path . '/form_theme.js',
    ),
);*/
$form['#theme'] = 'deductible_form';
return $form;
}


function deductible_theme($existing, $type, $theme, $path)
{
    return array
    (
        'deductible_form' => array(
         'arguments' => array('form' => NULL),
           'template' => 'deductible_form',   
         'render element' => 'form',   
    ),
    );
}
Template file
deductible_form.tpl.php

<div id="registration_form">
    <div class="field">
      <?php
        print 'hi'.drupal_render($form['first_name']);
      ?>
    </div>
</div>