How to use #theme for a form element in D6

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
govinda_roul's picture

Comments

Check From API QuickStart

pflame's picture

Check From API QuickStart Guide at http://drupal.org/node/751826. Drag to section Theming Forms, check 1st and 3rd points.

Code example for adding theme into form

shad_khan's picture

function my_module_form(&$form, &$form_state) {
$form['form_header'] = array(
'#type' => 'markup',
'#value' => 'TEST Theme',
);

$form['#theme'] = 'my_theme_template';

}

where 'my_theme_template' is your theme name , that will be included in theme hook in your .module file.

function my_module_theme() {
'my_theme_template' => array(
'template' => 'themes/my_theme_template',
'arguments' => array('form' => NULL, 'form_state' => NULL),
),
}

File structure

my_module
my_module/my_module.module
my_module/themes/my_theme_template.tpl.php

I hope this might help you.

Example for forms variables in theme template

shad_khan's picture

It is related to above example

How to use form and user defined variables in your theme template--

Content of themes/my_theme_template.tpl.php

<?php
echo $form_header;
?>

<?php
echo $my_var;
?>

for making available all the variables , easy way is, use preprocess function like this in your module file.
function my_module_preprocess_my_theme_template(&$vars) {
$form = $vars['form'];
$vars['form_header'] = drupal_render($form['form_header']);
$vars['my_var'] = 'Hello Test Var';
}

If you have any question please let me know

Thanx for your reply. I have

govinda_roul's picture

Thanx for your reply.

I have a module called "abc" and the abc.module contains the following code.

<?php
/<strong>
  *
Implementation of hook_menu().
  */
function
abc_menu() {
  
$items['abc'] = array(
     
'title' => 'View the abc form',
     
'page callback' => 'abc_page',
 
'access callback' => TRUE,
   );
   return
$items;
}
function
abc_page(){
 
//menu_rebuild();
  //drupal_set_message("Menu has been rebuid");
return "hello";
}
/</
strong>
*
Drupal form you can use any type of element here
*/
function
abc_form(&$form,&$form_state) {
 
$form = array();
 
$form['form_header'] = array(
 
'#type' => 'markup',
   
'#value' => 'TEST Theme',
   );
 
$form['search'] = array(
   
'#type' => 'textfield',
   
'#title' => t('Search here'),
  );
 
$form['#theme'] = 'abc_form';
 
$form['search_button'] = array(
   
'#type' => 'submit',
   
'#size' => 10,
   
'#value' => t('Search'),
  );
  return
$form;
}




function
abc_theme() {
  return array(
   
'abc_form' => array(
     
'arguments' => array('form' => NULL,'form_state'=>NULL),
     
'template' => 'themes/abc_form.tpl.php',
    
    ),
  );
}
?>

and template file called "abc_form.tpl.php" contains following code

<?php
/**
* Adding download page prepoces funton
*/
function abc_preprocess_abc_form(&$vars, $hook) {
 
$vars['search'] = drupal_render($vars['form']['search']);
 
$vars['search_button'] = drupal_render($vars['form']['search_button']);
 
// this is must
 
$vars['form_extras'] = drupal_render($vars['form']);
}
?>

// Search textfield
<?php
echo $search;
?>

// Search button
<?php
echo $search_button;
?>

// this is must
<?php
print $form_extras;
?>

But whenever I go http://localhost/abc ,it only shows me the "view the abc form" and "hello". The textfield and submit button disappeared.

Please help me.

Make these changes in your menu call

shad_khan's picture

function abc_menu() {
$items['abc'] = array(
'title' => 'View the abc form',
'page callback' => 'drupal_get_form',
'page arguments' => array('abc_form'),
'access callback' => TRUE,
);
return $items;
}

Many many thanx for your

govinda_roul's picture

Many many thanx for your quick reply.

Now I want to change the look and feel of my search button. How can I do ?

I added the following code to abc.tpl.php file

<?php
echo $search_button;
?>

But it is not reflecting.

Please help me.

I added < div style="color:

govinda_roul's picture

I added

<

div style="color: #A52A2A; font-size: 12px; font-weight: bold; width: 70px;">

Add css on class

shad_khan's picture

You can check the class name in firebug of your form submit button and that you can define in your css file.

I did the same thing. But the

govinda_roul's picture

I did the same thing. But the problem is , it inherits the css from style.css of the theme. If I do changes there then css of other elements also got changed. I want to change the look and feel of only the submit button not none of the other elements.

Please reply

Can it possible to add < div>

govinda_roul's picture

Can it possible to add

<

div> tags in form element. If yes how ?

Change your tpl file

pflame's picture

Change your tpl file abc_form.tpl.php to

<div>
<?php print drupal_render($vars['form']['search']); ?>
</div>
<div>
<?php print drupal_render($vars['form']['search_button']); ?>
</div>
<div>
<?php print drupal_render($vars['form']); ?>
</div>

The preprocessor function should always exist in your theme template.php file. You no need to write preprocess function here.

Thanx for your nice reply. I

govinda_roul's picture

Thanx for your nice reply.

I added the following to my abc.tpl.php file

<?php
/**
* Adding download page prepoces funton
*/
function abc_preprocess_abc_form(&$vars, $hook) {
 
$vars['search'] = drupal_render($vars['form']['search']);
 
$vars['search_button'] = drupal_render($vars['form']['search_button']);
 
// this is must
 
$vars['form_extras'] = drupal_render($vars['form']);
}
?>

<?php
print drupal_render($vars['form']['search']);
?>

<?php
print drupal_render($vars['form']['search_button']);
?>

<?php
print drupal_render($vars['form']);
?>

But the css is not reflecting.

Awaiting for your reply

govinda_roul's picture

<?php
print drupal_render($vars['form']['search_button']);
?>

you can define inherit classes in your abc tpl

shad_khan's picture

div class="my-form"
div class="my-form-1"

<?php
print drupal_render($vars['form']['search']);
?>

</div
div class="my-form-2"
<?php
print drupal_render($vars['form']['search_button']);
?>

</div
div
<?php
print drupal_render($vars['form']);
?>

</div
</div

CSS
div.my-form submit-id-name { color: #A52A2A; font-size: 12px; font-weight: bold; width: 70px;}

I hope this will help

you can add <div> tag in form api also

shad_khan's picture

$form['search_button'] = array(
'#type' => 'submit',
'#size' => 10,
'#value' => t('Search'),
'#prefix' => t('

'),
'#suffix' => t('

'),
);

you can class or id in above div , and implement unique css specific to this button. In t() function pass the html tag .

I tried the same thing but

govinda_roul's picture

I tried the same thing but till now the css is not reflecting. I checked the same with the theme developer module but the preprocess function is not showing there. Is there wrong with my above code. Plz suggest me.

Send me your code

shad_khan's picture

I want to see your code, send your code in zip format at shad.edims@gmail.com. So that I can point out the what is going wrong in that.