Theme Development: Overriding Theme functions and templates
This episode continues on where the Daily Dose of Drupal episode 40 left off and goes over how a Drupal 7 theme function and theme template that has been created in a module, can be overridden inside a Drupal 7 theme.
In this episode you will learn:
- How to override the output of a theme function inside your theme
- How to override the output of a theme template file inside your theme
Hello there everybody! And today we’re on Episode Number 41 of the Daily Dose of Drupal, I’m Shane, you can follow me on twitter @smthomas3 or you can come to the Code Karate website and check out my other post and sign up for the newsletter.
Today we’re going to continue on with where we left off last time and just do a real quick episode on how to actually override these Theme Functions and templates in your theme.
So this is kind of a little bit of a Theme Development tutorial I guess in a way. Basically; if you remember last time we created a My Example module which had a Theme Function … a very simple theme function, My Example Function here, as you can it also had this template. So we went through and created a Theme Function Module and a Theme Template file in our module.
So as you can see as if we come over to our test site here, if I go to a parents you can see I am using the default Bartik 7.15 default theme, I need to update to 7.16 but for now we’ll go with it and basically; I’m going to do this in the core theme file just for a real quick example, however; I am, going to be putting together an example here in the near future in how to create the Sub-Theme of Bartik because that’s the correct way to actually do it but generally you’re probably not going to be using a Core Theme or at least more times in that, you may have a custom theme or a theme that you’ve downloaded from drupal.org and so you may just be able to add it to the template file as I’m going to show you and add the file to the template’s directory and keep in mind that I’m doing this in the core theme, it’s not a best practice, it’s not the way you should be doing it and I will go ahead and make an episode a little later on how the correct way to sub-theme one of the core themes wouldn’t be.
So we’ll go ahead and get started; the first thing I’m, going to do is I open the Template file on the Bartik, you’re inside the Bartik Theme and in order to override this function which is what we want to do, I mean this is useful if you have … if there’s a module that has a Theme Function that outputs something in a certain way and you don’t want to obviously modify the module but you want o change the presentation of that Theme Function.
So as you can see if I go to this page on this site, the Theme Function is getting called to output this text here. So what I’m going to do is I’m going to come down here and I’m going to copy this Theme Function, I go into the template.php file of my current theme and I’m going to paste it in and I’m going to change the word “Theme” to the name of my theme, in this case Bartik, as you can see here I will then save this and what I’m going to do is I’m going to give it this a class, I’m just going to call this Bartik Override so we know that it is using the overwritten Theme Function.
So if I come back here and you take a look, first this does not have any class on it, I refresh the page, it still does not have any class on it and the reason being is because in order to actually get the results you generally going to have to save the … or excuse me, flush the cache. So we’re going to go ahead and do that just one second here; okay so I’ve pulled the command line, I’m just going to use Drush to clear the cache on the site, I’ll go ahead and could just clear one of the theme ones but we’re going to clear everything just to make sure it works, I should be able to refresh now and you can see if we scroll and look down in the HTML there’s a class Bartik Override.
That means now my … anything that is in my module in this function is not actually the function that’s being called, it’s being overwritten and this function is being used. So that’s on way to override themable output in your theme, is using the Theme Function.
The other is taking this template which I’ve already done and saving it inside … you can do it one of two ways; if I come back out I’m in the Bartik Theme, I could save it right here or I can save it in the template’s directory which I just did. So My Example_Template .tpl.php.
What this means is I can change this so that My-Example-Template will go ahead and pull it up so we can take a look at it before, you can see that it’s … the current class is My-Example-Template, I can change this to Bartik Template, save it, refresh, you can see we may have to clear the cache but you can see that this time it worked, the class is now Bartik Template, now instead of using the original template file, it’s using my overwritten template file inside my theme.
So those are two simple ways to override Theme Output; either from your module file or a function inside the module file or an actual Theme Template file which is a .tpl.php. So if you notice inside a module there’s a .tpl.php and it has HTML that you need to override instead of modifying it in the module file, you should make a copy of it, drop it into your theme and you can then override it there.
The same goes for … there are Theme Functions and you can find out if there’s a Theme Function or a Template file, you can look at yesterday’s episode where we go over it but if you look for the module name underscore them you’ll see all the available Theme Functions for … or that can be overwritten.
In this case it’s just a function but in this you can see that it’s clearly a Template file so you can be on the lookout for that. So that is how you can override Theme Output, that’s it for this time on the Daily Dose of Drupal, follow me on Twitter and we will see you next time, thanks for watching.