Introduction Advanced Theming in Drupal 7

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi my name is Michelle and welcome to advanced theming in the previous series we discussed dot info files which are the configuration files for your themes which tells Drupal that you are a theme what regions you have what CSS files you have and what JavaScript you have we also discussed template files those are the files ending in TPL dot PHP like your page TPL or your node TPL but in this series we're actually going to talk about template dot PHP this is the file where all of your functions live and we are going to discuss pre process functions and overriding theming functions so what if there isn't a variable for what you want to do in your page TPL or in your node TPL you've already done a dump of all possible variables and it's just not there you've got something else unique and custom well I'm sure you've heard this on multiple occasions but you actually don't put complicated PHP code in your TPL files you put that somewhere up complicated logic in your template dot PHP so we are actually going to discuss today how to create those new variables and override existing ones so welcome to template dot PHP the theming new ninjas weapon of choice so here like I said we are going to set new variables that you will have access to in any any of your TPL dot PHP files so template dot PHP it's all PHP code there is no HTML it's the exact opposite of a TPL dot PHP it's the place where you put extensive logic and even sometimes write SQL queries by separating out all of this PHP functionality into a unique file now keeping your note TPL and your page GPL files very very clean it actually makes it easier for a designer to come in and change the mark-up and work with the CSS that's associated with that template page fee like I said before provides all of the variables for you that you have access to in your in your TPL files and also we can go in and we can override some of Drupal's functionality there are several functions like theming a username that we can go in and say you know Drupal does it pretty well but I've got I want to do a little bit better I want to add another character in there change the mark-up a little bit and in your a template dot PHP you can override any of those first thing I want to talk to you about our pre process functions this is where you create new variables or override existing variables the first function is commonly known as the theme name underscore pre-process now you would replace theme name with the name of your theme for example Bartek underscore pre-process or you can go ahead if you are you following our tutorial do 960 robot's underscore pre-process this function gets called all the time it's the biggest global function to save on resources you actually can go ahead and do a switch and check and say okay well if it's actually a page let's go ahead and call that function it can be even more specific there is theme name underscore pre-process underscore hook where that hook would be replaced with something like page or node so any times a page is loaded your Bartek underscore pre-process underscore page will get called you'll notice that in both of these examples dollar selling VARs is being passed in and that is where all of your variables are that then at the very end gets sent to your template files so you can go ahead and say dollar VARs my new variable equals some sort of value and that will then be okay available to you in your other files another thing that template dot PHP is useful for is overriding theme functions the most common example would be theme username that is for example in the top of the node and say o submitted by Jo on this date so the submitted by Jo part the way that Jo is written that's their username so if you don't like how that's being handled you can go ahead and copy that function from corynn and rename the theme username to whatever yours is so it could be Bartok underscore username and then it gets called so very simple you go ahead you find the original function you copy it into your template dot PHP file and you rename any of the prefixes and make amendments as you need fit don't forget to clear the cache though so the way Drupal looks at theme functions is in the code in a module theme breadcrumb is written and immediately it goes and looks and says hey does the current theme have any specification about what that means so it goes and looks for my theme underscore breadcrumb if it finds it great go ahead and use it and that's the output if it doesn't find it it then moves on to the default of theme underscore breadcrumb and whatever the output of that is previously in Drupal 6 there was also a PHP template breadcrumb but you don't need to worry about that any longer so theme functions allow your theme to have the last crack at the output at HTML and you have complete control over that just by naming your function following a certain pattern that's what gives you the power so theme functions themselves are never directly called you never call theme underscore username you never even type in your module is to call a specific variation of it AB Arctic underscore username you always utilize the syntax of printing theme parentheses and then in single quotes whatever type of theme function that is and the reason you do that is that actually triggers the Drupal system to go ahead and to look for does the current theme have an implementation of this and to utilize that first and if not then to go find what the default behavior should be very very very powerful the theme override system allows you to completely control all HTML output just by typing something very very simple in your template dot PHP file so the theme function itself will always call the correct version so when do you know to use a TPL dot PHP file or to use your template dot PHP file with theme functions and pre process functions first bit to think about at TPL dot PHP is for markup it's for HTML and very very simple print statements only this is the kind of file a designer could pick up and go okay I need to change this div to an h2 and work with that the theme functions in template dot PHP and pre process functions in tempo dot PHP are very very PHP code heavy Limited markup in there because whenever possible your modules are also going to be providing these TPL files and then your themes can override them complex logic always will take the place in your template PHP to create a variable to pass to your TPL ph PS just to recap what we have done in the previous series and quit what we're going to talk about going forward is there are three steps to theming the first is your dot info file this is where you say hey I'm a theme I have these different kinds of CSS files and JavaScript files then there's also the template files this is your page template and your node template or even your node article templates and then going forward in our advanced theming series we are going to discuss template dot PHP where we can create new variables to pass to our template files where we can change variables and also where we can adjust markup for simple functions that are used regularly like theme username or theme breadcrumb I want to take a moment to compare the two different kinds of files the TPL files and template dot PHP if we take a look at Bartok the theme that comes with Drupal core and we go ahead and open our page TPL and our template dot PHP we will be able to notice some differences for our page TPL the biggest thing you're going to notice is that we threw out the file have opening and closing PHP tags so this line few lines right here is a PHP statement that says if Dollar logo exists then we need to print what's on the following lines and then simply saying ok that's the end of our if statement so a TPL dot PHP file is full of if a variable exists print something potentially that variable with markup around it and then you end if statements there's no complicated logic in here differently from that if we open our template dot PHP file you'll notice that there is an opening PHP tag and that is it all of the contents of this file is 100% pure PHP there's no markup in here there are functions followed by functions followed by functions in this file and what I wanted to really show you is that if we go back a designer can open up a TPL dot PHP file and very easily say you know what I really don't want this to have a div ID of site slogan I want it to be something else and go in and make those changes to the file without any problems whatsoever there's no complicated logic going on if you go to a template dot PHP file there's no markup so this is where variables are being set and and also changed the biggest thing to remember with theming in Drupal markup goes in TPL dot PHP and then PHP logic goes in tempo dot PHP and I find it very very confusing that the nomenclature for these are so similar so just remember a template file is your page TPL your node t PL and template dot PHP is where all of your functions live
Info
Channel: Drupalize.Me
Views: 12,870
Rating: 4.875 out of 5
Keywords: Theming, template.php, drupal, Drupal theming, Drupalize.Me, Drupal 7, page rendering, theme developer tool, new theming hooks, Drupal tutorial, Drupal learning, learn drupal, Drupal step by step, Drupal videos, Drupal video, Lullabot
Id: TisFAOb8wn4
Channel Id: undefined
Length: 12min 24sec (744 seconds)
Published: Mon Dec 12 2011
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.