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;
}

No comments:

Post a Comment