Drupal Theme Hooks: a Simple Example
By default, Drupal adds the text "Not Verified" to comments from unregistered users. It also adds the rel="nofollow" attribute to the links they provide. I didn't like either one of those things so I set out looking where to fix it. It turns out that both these bits happen inside the function theme_username(). This function is hookable. That is to say, this function can be replaced by a custom function in a theme.
My current theme (at the time of writing) is Interactive Media (here's a demo). I like the look of it but of course I'm going to continue tweaking and rewriting. This theme uses the phptemplate engine. Why am I telling you this? Because it determines the name of the function that will override the default theme_username(). I called mine phptemplate_username() - the recommended practice. If I didn't know that my theme is using the phptemplate theming engine then I could equally well call it interactive_media_username().
Now that the name is settled, just grab template.php from the them and add the function. In my case, as I expect is the case for many others, I started from the default implementation of theme_username(). Just copy the function body & paste it in with the new name. The subtle detail to be aware of here is that you're inheriting any bugs (security holes and whatever) that the default implementation has when you do this. I suppose if you're really on the ball then you can make note of these hooks and pay attention in future patches
Comments
Post a Comment