using hook_theme to style module output

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

Hello everyone,
I am writing a module that requires quite a bit of theming and I want to be able to use a tpl to control the layout of the page callback on a menu item. I have tried using the theme() function like so but I am getting a blank screen in response:

in the menu code: 'page callback' => 'featured_page',

the callback function:

function featured_page () {
$hello = "Hello world";
return theme("main_page", $hello );
}

the theme function:

function bookeditions_theme() {
$path = drupal_get_path('module', 'bookeditions');
$theme['main_page'] = array(
'arguments' => array('hello' => NULL),
'template' => 'bookeditions',
'path' => $path,
);
return $theme;
}

I have tried dozens of other arrangements and nothing works... at this point I am running out of smart guesses. Is there anyone that could explain how to do this properly? I can't find any tutorials that make it clear.

Comments

wrong function

chrisfromredfin's picture

You need to make a function called theme_main_page($var)
.cw.

Reason why

dwees's picture

You call theme("main_page", $hello ) and the theme() function in turns look for a function called theme_main_page, and if it exists, will use that theming function. Otherwise Drupal will have no idea what function to use.

As for your function naming, it is a good idea that any new functions you create are in your module's 'namespace'. So if your module name is 'bookeditions' you should prefix your 'public' functions with the name of your module, and your internal 'private' functions with '_bookeditions'. This is just a matter of style of course, but it will obviously help prevent your functions from being accidentally duplicated in another module.

Putting it all together

PixelClever's picture

I know a lot of time has passed since I first posted this question, but since then I have managed to get these theming functions working on several modules and I thought I might as well comment on what was causing the problem for me.

What was confusing me was the theme trio:

  1. hook_theme which adds an array of callbacks and their arguments to the theme registry. I didn't realize that I had to visit the modules page to rebuild the theme registry before it would be added.
  2. The themable function itself which starts with theme_ followed by the function name that was added to the registry with hook_theme
  3. theme("whatever_function, $whatever_argument, $whatever_argument ") which actually calls the function.

All of these "theme" functions were getting mixed up for me and I wasn't using them properly. Now it makes sense though. The main resource that clarified this for me was the Pro Drupal development 2nd edition which really helped because it showed several examples using the Drupal 6 hook_theme.

Web design, Drupal theming, and logo design:
http://www.translationdesigns.com

Drupal theming, Module development and logo design:
http://www.PixelClever.com

Agree - thanks for the update

bkudrle@gmail.com's picture

Thanks for the update Aaron. I too have found the Pro Drupal Development book (2nd edition) the most helpful. Your comment about the need to revisit the module page every time the theme section was changed was VERY useful and saved me much frustration. Thanks for the notes.