New hook_theme() in Drupal 6

As of Drupal 6, when you're writing modules, you might find the theme() function doesn't work as you expect. Whereas before you'd pass a parameter to the theme() function that told it which theme_* function to call, now you have to register the theme_* function for it to actually get called.

Not doing so probably results in mysterious data loss during execution for most implementations. I've known about it for some time now and still forget to register my theme_* functions.

There are so few examples at the moment on how to register theme functions, I wrote a stub module with just enough code to show the registration at work. I've made it available for download as a .tgz for you to play around with on your own.

<?php/** * @file * Demonstration of theme function registration for Drupal 6 * * @author David Kent Norman *//** * hook_menu() * * @return array */function themedemo_menu() {  $items = array();  $items['themedemo'] = array(    'title' => 'Theme Demo for Drupal 6',    'page callback' => 'drupal_get_form',    'page arguments' => array('themedemo_example'),    'access callback' => 'user_access',    'access arguments' => array('access content'),    'type' => MENU_NORMAL_ITEM  );  return $items;}/** * Demo form to have something to theme * * @return array */function themedemo_example() {  $form = array();  $form['example'] = array(    '#title' => t('This is just an example'),    '#type' => 'checkbox',    '#default_value' => FALSE  );  return $form;}/** * hook_theme() * New registration function introduced in Drupal 6 * * @return array */function themedemo_theme() {  return array(    'themedemo_example' => array(      'arguments' => array('form' => NULL)    )  );}/** * The function that has to be registered in hook_theme() * * @param array $form * @return string */function theme_themedemo_example($form) {  $form['example']['#description'] = t('This description was added by the registered theme function.');  $output = drupal_render($form);  return $output;}?>

AttachmentSize
themedemo.tgz787 bytes

theming forms

Hi,

In the example shown above can you let me know how to pass additional variables to the theming function for a form ?

I am trying to convert a D5 module to D6. Please ve a look at my following post.

http://drupal.org/node/240947

Thanks
Karthik

reply

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Allowed HTML tags: <hr /> <a> <p> <em> <strong> <cite> <code> <blockquote> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
Your post will remain unpublished until David examines it himself to check for spam.