Syndicate

Feed

Theming Drupal 6 from the module layer

Sometimes, you need to change the HTML markup output by some module, and you want your changes to keep whichever theme you will use. The need came up for me recently when I had to provide special markup for pages that display a webform. I had to quickly put together a module for a client, a module that would provide a template file, which I decided to name (arbitrarily) page-webform.tpl.php.

The situation may arise for you as well, so I will share my technique with you. Say you want to provide a template file for all pages that display one node of a content type with machine-readable name CONTENT_TYPE_NAME. And say you want to use a module to provide such template. Say you decide to name your module special_page.

You start with creating a DOT info file for your module. The name of which would be in our case special_page.info.

;$Id$
name = Special Page
description = Provides a template file for my content type CONTENT_TYPE_NAME.
core = 6.x

Then you go about creating your DOT module file, ie: special_page.module.

<?php
// $Id$
/**
 * @file
 * Module that provides a special template for pages that show 
 * ONE node of content type CONTENT_TYPE_NAME.
 */

You probably should implement HOOK_help() in your module, but I will skip that, and cut the chase to what we really need.

At this point, you need to tell the theme system to use your template, and to use it only in certain situations. Your 'situations' will differ from mine. In my case, I wanted the theme system to use my template on a node page, hence any page with path node/nid, yet only on pages that show the node in view 'mode', so I did not want the template to be used when the path is node/nid/edit for example. There are many ways to skin the cat here. I decided to use the fact that a $node object is passed to the page.tpl.php template only when the page is a node page, as you will see in the following code snippet. Here, I will make use of a preprocess function to pass on to the theme system a suggestion about a new module-supplied template.

function special_page_preprocess_page(&$variables) {
  // If this is a node page (not a list of nodes page) and
  // the node is shown in 'view' mode rather than 'edit' or whatever.
  if (isset($variables['node']) && (arg(2) === NULL)) {
    // If the content type of that one node is 'CONTENT_TYPE_NAME'.
    if ($variables['node']->type == 'CONTENT_TYPE_NAME') {
      $variables['template_file'] = 'page-CONTENT_TYPE_NAME';
    }
  }
}

Then feel free to create such template file. As a reminder, no need to add the tpl.php extension to the value you assign to $variables['template_file']. Also, that name can be anything, I am just following conventions here by prefixing with page, as in page-SOMETHING.tpl.php. Make sure that the name you provide here matches the name of your template file.

Then, you are faced with a small problem: the template file will need to be placed in the theme folder in order to be picked up by Drupal's theme system. But you don't want that. So what to do? Here comes a situation where we can use the module hook HOOK_theme_registry_alter(). For the theme hook you want to provide special theming for (hook in theme parlance here), you will have to tell Drupal Hey, Drupal, please look in my module folder over here, you may find a template file you will need.

EDIT: As of Drupal 6.7, this last step should no longer be necessary. See this page in the Theming Guide on Drupal.org for details. I haven't had the chance to re-test my module without the following snippet.

function special_page_theme_registry_alter(&$theme_registry) {
  $theme_hook = 'page'; // my hook name
  // Get the path to this module
  $modulepath = drupal_get_path('module', 'special_page');
  // Add the module path on top in the array of paths
  array_unshift($theme_registry[$theme_hook]['theme paths'], $modulepath);
  // dsm($theme_registry[$theme_hook]['theme paths']);
}

And you are done.

A word on performance

What's more...

The hook function HOOK_theme_registry_alter() is called only when the theme registry is rebuilt, that is, when you clear your Drupal cache. (It is called for all modules that implement it of course.)

The hook is NOT called at every page refresh :-)

What a relief.

To test this, uncomment the dsm() call.

Last edited by Caroline Schnapp about 12 years ago.

Comments

Great Post. I have not been

Great Post. I have not been visiting the site recently. Took a visit again and there were some great comments on the site. Excellent post.
Keep up the good work. J Gateway Duo Residences

I found so many entertaining

I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the leisure here!

Bless you for taking a few

Bless you for taking a few minutes to publish this. I do believe that there are more desirable solutions. http://www.truthaboutnononsensemusclebuilding.com/

scary

this is scary. i do no wan to be trapped inside again. lush acres ec site plan punggol new ec executive condominium

drupal

drupal is such a cool item to use.

I really like your blog. It

I really like your blog. It has highly-researched information that's why it really grabbed my attention. Thank you so much for sharing this post.
tp180 showflat
visit tp180 here for more info

I really admire your blog.

I really admire your blog. The content was really useful. Thank you so much for sharing with us some valuable information about theming Drupal 6 from the module layer.

I gained a lot of useful

I gained a lot of useful information from your post. I was really amazed with all the information that is on it. Thank you so much for sharing this post.
TP180 Guocoland
http://www.youtube.com/watch?v=Lu4IUfW2qK4

Great

Great piece of article..

Belgravia Villas

I really enjoyed the quality

I really enjoyed the quality information you offer to your visitors for this blog. I will bookmark your blog and have my friends check up here often.
http://guocolandtp180.org
watch tp180

Hi, I enjoyed reading your

Hi, I enjoyed reading your blog post. It's really interesting. Thanks for sharing this post.
http://guocolandtp180.org
watch tp180

This is the best site to

This is the best site to spent time on. I just stumble upon your chatty blog and wanted to say that I really enjoyed reading your very well written blog post. Thanks for sharing this post.
http://guocolandtp180.org
watch tp180

The Panorama is also near to

The Panorama is also near to Nex Shopping Centre as well as the buzzling Toa Payoh area. Entertainment for your loved ones and friends is therefore at your fingertips with the full condo facilities as well as the amenities around Ang Mo Kio. ang mo kio Panorama

Entertainment for your loved

Entertainment for your loved ones and friends is therefore at your fingertips with the full condo facilities as well as the amenities near The Inflora Condo. Inflora Condo

Greetings! I know this is

Greetings! I know this is kinda off topic but I was wondering if you knew where I could find a captcha plugin for my comment form? I'm using the same blog platform as yours and I'm having trouble finding one? Thanks a lot!One Tree Hill Season 9 DVD Box Set | Game of Thrones Season 3 DVD Box Set | Friends Seasons 1-10 DVD Box set | American Horror Story Seasons 1-2 DVD Box Set

There are many different

There are many different instruments to groom dogs. Among them is the Curry Brush, Shedding Blade, Stripping Knives, Slicker Brushes and Rakes. These tools are very common among dog groomers and every pet shop should be able to supply. Dog Grooming singapore

Riverbank at Fernvale is a

Riverbank at Fernvale is a 99-years leasehold private condominium development located atSengkang West Way / Fernvale Link in District 19. With expected completion in mid 2017, it comprises of TBA towers with estimated 590 units and stands TBA storeys tall. Riverbank at Fernvale

Riverbank at Fernvale is a

Riverbank at Fernvale is a 99-years leasehold private condominium development located atSengkang West Way / Fernvale Link in District 19. With expected completion in mid 2017, it comprises of TBA towers with estimated 590 units and stands TBA storeys tall. Riverbank at Fernvale

Great blog. All posts have

Great blog. All posts have something to learn. Your work is very good and i appreciate you and hopping for some more informative posts. Thanks for such a wonderful article. I would like to review what you wrote along with link to your post. My favorite part of the article was truly in the different techniques towards handling this problem. Now, because I really like your article. Game of Thrones Season 3 DVD Box Set | Friends Seasons 1-10 DVD Box set | Breaking Bad Season 5 DVD Box Set | Mad Men Season 6 DVD Box Set | CSI: NY Seasons 1-9 DVD Box Set | Walt Disney’s 100 Years Of Magic 164 Discs DVD Box Set

Boardwalk Residences in Sengkang Fernvale Close

It is situated right beside Layar LRT Station. Future residents will be able to access the nearby Compass Point and Greenwich V which is a short drive away for some family fun and gatherings. A truly unique lifestyle awaits you. Boardwalk Residences

Boardwalk Residences in Sengkang Fernvale Close

The condo’s facilities provide full family entertainment needs for your family and loved ones. Indulge in a serene and tranquil lifestyle right in the heart of Sengkang. Boardwalk condo sengkang west way

Rivertrees Residences is

Rivertrees Residences is also near elite schools such as Pei Hwa Secondary School, Nan Chiau Primary School, Nan Chiau High School. Rivertrees Residences in Fernvale Sengkang

Pet Grooming Centres in Singapore

If you happen to a dog or two, then you can try the canned fish such as salmon or tuna as a healthy alternative for your dear pet. Fish can be an extremely nutritive part of your dog’s regular diet because it is an excellent source for a number of essential nutrients. Fish Diet

Lakeville has full and

Lakeville has full and unique facilities, which includes a guard house, clubhouse, children’s playground, swimming pool, Aerobic/Yoga room, piano room, pool room, indoor gym, hydrotherapy beds, hydrotherapy baths, reading room, function room, onsen, jacuzzi. launch condo

Highline Residences will be

Highline Residences will be accessible via Tiong Bahru MRT. Commuting to the city area is therefore very convenient and the amenities are at your finger tips. It is also right beside Tiong Bahru Plaza and Central Plaza. IMM Jurong as well as Jurong Country Club are also within a short walk. highlineresidence.net

Several buses are available

Several buses are available near The Santorini along with shopping centers and restaurants. The Santorini is also near to Century Square and Tampines Mall. new executive condo