Thursday, March 10, 2011

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>

2 comments:

  1. Gracias, En verdad me sirvió de mucho. Thank you

    ReplyDelete
  2. Estaba buscando alguien que me confirmase esto, Gracias.

    ReplyDelete