drupal form api theming!

pradeepg_jain - August 28, 2009 - 08:46

<?php
function theme_test_form($form) {
 
$output = '';
 
$output .= drupal_render($form['name']);
 
$output .= '<div id="foo">';
$output .= drupal_render($form['age']);
 
$output .= '<div class="messages">';
 
$output .= drupal_render($form['details']);
 
$output .= '</div></div>';
 
$output .= drupal_render($form);
  return
$output;
}
function
test_form($form_state) {
 
// Access log settings:
 
$form['firstname'] = array(
   
'#type' => 'textfield',
   
'#title' => t('First Name'),
   
'#size' => 30,
   
'#maxlength' => 64,
   
'#description' => t('Enter Name'),
   
'#required' => TRUE,
  );
 
$form['age'] = array(
   
'#type' => 'textfield',
   
'#title' => t('Age'),
   
'#size' => 30,
   
'#maxlength' => 64,
   
'#description' => t('Enter the Age'),
   
'#required' => TRUE,
  );

$form['place'] = array(
   
'#type' => 'textfield',
   
'#title' => t('Place'),
   
'#size' => 30,
   
'#maxlength' => 64,
   
'#description' => t('Enter place'),
   
'#required' => TRUE,
  );
 
$form['hidden'] = array('#type' => 'value', '#value' => 'is_it_here');
 
$form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
  return
$form;
}

function
test_page() {
  return
drupal_get_form('test_form');
}

function
test_form_validate($form, &$form_state) {
 
/*if ($form_state['values']['firstname'] == '') {
    form_set_error('', t('You must enter your name.'));
  }
  if ($form_state['values']['age'] == '') {
    form_set_error('', t('You must enter your age.'));
  }*/
}
function
test_form_submit($form, &$form_state) {
 
db_query("INSERT INTO {new_form} (name, age) VALUES ('%s', %d)", $form_state['values']['name'], $form_state['values']['age']);
 
drupal_set_message(t('Your form has been saved.'));
}
print
test_page();
?>

i used the code from the http://api.drupal.org/api/drupal/developer--topics--forms_api.html/6 and the form gets displayed and everythings working fine .but the theme_test_form is not working even if i have added the corresponding css in theme.css file. IS there any additional work to be done for css to get applied or theme change to get applied..

Thanks,
Pradeep

Hey Pradeep, You need to add

gbernier - October 30, 2009 - 05:44

Hey Pradeep,

You need to add $form['#theme'] = 'test_form'; This tells the functions that process the form to call theme_test_form

Cheers,
Gene

Gene Bernier
Web Development Supervisor
Acro Media Inc
http://www.acromediainc.com

Adding theme to custom form...

arunms - November 10, 2009 - 02:36

Hi Pradeep/Gene:
I added the #theme attribute to the form, but still couldn't get the form to call the theme function. Is there any additional step involved in calling the custom theme function.

Thanks.
Arun M

Thanks
Arun M

"Satisfaction lies in the effort, not the attainment. Full effort is full victory"

Have you registered the theme

Jay Matwichuk - November 10, 2009 - 02:43

Have you registered the theme function in hook_theme()?

I just did...

arunms - November 10, 2009 - 17:57

I just registered the theme and it works like a charm.

Thanks
Arun M

"Satisfaction lies in the effort, not the attainment. Full effort is full victory"

 
 

Drupal is a registered trademark of Dries Buytaert.