Introduction to Theming Basics for Drupal 7

Video loading...

  • 0:00
    [robotic voice] Drupalize.Me. [drupalize.me - Learn Drupal]
  • 0:04
    [♪♪] [Introduction to Drupal Theming - Theming for Drupal 7, Chapter 1 with Joe Shindelar]
  • 0:15
    In this chapter we'd like to talk a little bit about what theming in Drupal really is.
  • 0:20
    First off, the word theming is pronounced kind of like scheming
  • 0:23
    and spelled just like it's shown on the screen.
  • 0:26
    Spellcheckers tend to get a little bit confused by it, but that's really the way that we say it:
  • 0:30
    theming.
  • 0:31
    Theming in Drupal is made up of a number of different pieces.
  • 0:35
    One of the things that you're doing is designing the appearance of your website.
  • 0:38
    You might use tools like Photoshop or Fireworks
  • 0:41
    to mock up what the website is going to look like for your users.
  • 0:46
    Another component of theming in Drupal is creating the CSS
  • 0:49
    to control the layout of your site.
  • 0:51
    This isn't something that we're going to cover in this video series.
  • 0:55
    However, creating the CSS is no different
  • 0:57
    than what you would do for creating a static HTML website
  • 1:00
    or creating the CSS design for any other website.
  • 1:03
    The last part, and the part that we'll talk about the most in this video,
  • 1:07
    is changing the HTML to output by Drupal to fit the design.
  • 1:12
    When Drupal creates a page, it outputs a set of default HTML markup.
  • 1:16
    What we're going to talk about is how you as the themer
  • 1:19
    can modify that HTML output by Drupal to conform to the HTML
  • 1:23
    that you need for your designs.
  • 1:27
    There's 3 simple steps to creating a theme in Drupal,
  • 1:31
    and we'll talk about each one of those in depth.
  • 1:33
    The first step is to create an info file.
  • 1:36
    Info files are metadata that tell Drupal what your theme does and what its name is.
  • 1:41
    The second step are template files
  • 1:44
    or any file that ends in a .tpl.php extension.
  • 1:48
    And the third step, template.php, is a php file, optional for themes,
  • 1:54
    used to replace the variables inside of template files and override theme functions.
  • 2:00
    We won't be talking about the third step in this basic theming series.
  • 2:04
    However, in a coming advanced theming series we'll go more in depth on step #3.
  • 2:10
    Let's talk a little bit about the structure of a theme.
  • 2:14
    All of our themes--those that we download from Drupal.org or those that we create ourself--
  • 2:19
    live inside of the sites/all/themes directory
  • 2:22
    in a directory that is the name of your theme.
  • 2:25
    Themes are made up primarily of things that are part of traditional web design--
  • 2:30
    things like CSS files, JavaScript files,
  • 2:33
    and images that make up all of the layout of the page.
  • 2:36
    All of these files live inside of your themes directory.
  • 2:39
    And in addition to that, you'll see some files that are specific to creating a theme in Drupal--
  • 2:44
    things like an info file, optional template files, and an optional template.php file.
  • 2:53
    Again, the .info file corresponds to step #1 in creating a theme,
  • 2:58
    telling Drupal what your theme can do.
  • 3:00
    Template files, like the page.tpl.php file, which are primarily HTML,
  • 3:06
    control the overall layout of your site, and the general HTML markup
  • 3:10
    correspond nicely to step #2.
  • 3:13
    And finally, template.php, which is used to set variables and override theme functions,
  • 3:17
    corresponds to our step #3.
  • 3:20
    Let's take a look at what you might see inside of an info file.
  • 3:24
    The primary thing that an info file does
  • 3:27
    is announce the presence of your theme to Drupal.
  • 3:29
    Without a .info file, Drupal will not know that you've created a custom theme
  • 3:33
    and you will not be able to enable it.
  • 3:36
    Inside of your info file are a bunch of key value pair properties
  • 3:39
    that do things like tell Drupal the name and description of your theme,
  • 3:42
    define some of the CSS and JavaScript files that you might use,
  • 3:46
    and, in addition to that, tell Drupal about features that your theme might provide
  • 3:49
    and the regions that your theme makes available for placing blocks into.
  • 3:54
    A typical info file might look something like this--
  • 3:57
    again, key value pairs representing things like the name of your theme
  • 4:02
    and the description and so forth.
  • 4:05
    In Drupal 7 the only 3 required things in a .info file are these first 3:
  • 4:10
    the name, which provides a human readable name for your theme,
  • 4:14
    and a description, which is just a simple 1-sentence description of your theme.
  • 4:18
    These things are used on the appearance page when enabling a theme
  • 4:22
    to provide administrators with a friendly name and description for your theme.
  • 4:26
    In addition to that, you also need to specify the version of Drupal core
  • 4:29
    that your theme is compatible with.
  • 4:32
    In this case we're saying that our theme is compatible with Drupal 7.
  • 4:36
    One of the things that you'll commonly see in a .info file
  • 4:40
    are these style sheets and scripts lines.
  • 4:42
    This is our theme using the .info file in order to include JavaScript files
  • 4:47
    and CSS files on every single page throughout the site.
  • 4:51
    This is nice for things like CSS files which provide the main layout for the page
  • 4:55
    and that you want to be loaded on every single page.
  • 4:58
    Declaring style sheets and JavaScript files in a themes.info file
  • 5:03
    also ensures that Drupal is able to use CSS and JavaScript aggregation
  • 5:07
    to include your themes, JavaScript, and CSS files.
  • 5:12
    The syntax for including style sheets and scripts is similar to the name and description syntax.
  • 5:16
    However, in this case we use square brackets, similar to creating arrays in PHP.
  • 5:21
    This allows us to add multiple CSS files or multiple JavaScript files.
  • 5:25
    Finally, the style sheets line.
  • 5:28
    You can see the word all in the first set of square brackets.
  • 5:32
    This maps to the media attribute and the link tag used when including a style sheet on the page,
  • 5:37
    so you could, instead of using all, use words like print
  • 5:40
    and you would end up with a style sheet that had a media attribute of print
  • 5:43
    and would only be used when someone is printing the page.
  • 5:46
    In addition to adding style sheets and JavaScript files through your .info file,
  • 5:51
    you can also add style sheets and JavaScript in a template.php file.
  • 5:56
    You would do this in scenarios where you had certain style sheets
  • 5:59
    that you only wanted to load on certain pages throughout the site.
  • 6:02
    We'll talk more about that in another video.
  • 6:06
    In Drupal, themes are responsible for declaring all of the regions that are available
  • 6:12
    for an administrator to place blocks into.
  • 6:15
    Regions are declared inside of a .info file.
  • 6:18
    The syntax for creating regions inside of a .info file
  • 6:22
    is similar to creating an array in PHP.
  • 6:25
    You type the word regions
  • 6:27
    and then inside of square brackets the machine readable name of a region.
  • 6:31
    And then the value is the human readable name of the region.
  • 6:36
    A couple of important things to note when creating regions in a Drupal theme.
  • 6:40
    First, regions are optional.
  • 6:42
    You don't actually need to add any regions in your theme.
  • 6:45
    If you don't add any regions, Drupal will assume a default set of regions.
  • 6:50
    The default set of regions include a sidebar_first, sidebar_second, content,
  • 6:55
    header, footer, highlighted, and help.
  • 6:58
    As soon as your theme declares 1 region, the defaults are ignored
  • 7:03
    and Drupal assumes that your theme will declare all of the necessary regions.
  • 7:08
    One important thing to point out for Drupal 7 themes
  • 7:11
    is that the content region is required in order for your theme to be enabled.
  • 7:16
    Once you've added regions to your theme,
  • 7:19
    they become available for an administrator to place blocks into.
  • 7:22
    And it looks something like this.
  • 7:25
    You can see I've declared a sidebar_first region,
  • 7:28
    and on the block administration page I now have an option to add
  • 7:31
    any blocks to that first sidebar, the human readable name there.
  • 7:36
    And I also have inside of my page template file in the page variable
  • 7:41
    the sidebar_first region, which will contain all of the blocks that are in that region
  • 7:46
    for that particular page.
  • 7:48
    Another thing that you may or may not see inside of a themes.info file
  • 7:53
    is the declaration of features.
  • 7:55
    Features allow you to create single boolean variables
  • 7:58
    and a checkbox that maps to those variables on the theme setting page.
  • 8:02
    You might create a variable for something like logo or slogan
  • 8:06
    that inside of your page template file or other template files
  • 8:09
    you can check and see, "Did the administrator of the site click the checkbox
  • 8:13
    "that corresponds to my logo feature?"
  • 8:15
    If so, I may want to print out some additional markup on the page.
  • 8:19
    If not, I may want to skip printing out that markup.
  • 8:23
    Again, if you don't provide any features in your theme,
  • 8:27
    Drupal assumes a default set of features.
  • 8:29
    Those defaults are logo, name, slogan, node_user_picture,
  • 8:34
    favicon, main_menu, and secondary_menu
  • 8:38
    and the corresponding checkboxes to turn those things on and off
  • 8:41
    when configuring settings for your theme.
  • 8:43
    Those are the most common things that you'll see inside of a .info file.
  • 8:47
    There are a few additional things, but we'll talk more about those
  • 8:50
    when we actually get to creating a .info file and editing some of those configuration variables.
  • 8:56
    The next thing that you'll take a look at with theming are template files.
  • 9:00
    Template files in Drupal are where the real theming work happens.
  • 9:05
    Template files, or any of these files that end in a .tpl.php extension,
  • 9:12
    contain mostly HTML and a little bit of PHP.
  • 9:17
    What they are are HTML files that contain simple print statements
  • 9:22
    to print out the dynamic content provided by Drupal wrapped in HTML
  • 9:27
    that creates the structural markup of your page or things like nodes, blocks,
  • 9:33
    or any element that's displayed on the page.
  • 9:36
    Template files control the overall layout and markup of your site.
  • 9:40
    The majority of things that you will do in theming take place inside of a template file.
  • 9:46
    Here's an example of how these template files work.
  • 9:50
    When looking at a page rendered by a Drupal theme,
  • 9:53
    it's actually made up of a number of different template files.
  • 9:56
    The wrapping portion of the page--things like the opening and closing div
  • 10:01
    that may contain all of the different regions, the header and the footer and so forth--
  • 10:06
    live inside of a template file called page.tpl.php.
  • 10:10
    That template file is used once to theme the entire page.
  • 10:14
    The different components of the page, however, may be run through their own template files
  • 10:18
    before being printed out.
  • 10:20
    For example, in our mockup here we have 2 regions with blocks in them.
  • 10:25
    The one on the left shows all of the nodes on this front page,
  • 10:28
    and the one on the right shows a number of additional blocks,
  • 10:31
    presumably placed there by the administrator.
  • 10:33
    Each of those blocks is rendered inside of a region,
  • 10:36
    and that region has a corresponding region.tpl.php.
  • 10:41
    Inside of that region you have things like all of the nodes
  • 10:44
    that make up the center content of the page.
  • 10:48
    Each one of those nodes, when it's printed to the page, uses the node.tpl.php file,
  • 10:54
    and each one of those blocks over on the right, those use a block.tpl.php file.
  • 11:00
    It's important to note that some of these template files--
  • 11:03
    things like node and block, for example--may be used more than once
  • 11:07
    when presenting a single page.
  • 11:09
    This makes it really easy to create markup for something like a node
  • 11:13
    and have it be reused for every single node every time it's printed out.
  • 11:18
    An example of a template file might look something like this.
  • 11:22
    This is the node template file from the Bartik theme that ships with Drupal 7.
  • 11:28
    You can see there's definitely some PHP in there,
  • 11:32
    but it's limited to primarily simple print statements
  • 11:34
    and a couple calls to this render function
  • 11:37
    and then a bunch of HTML that makes up the main markup
  • 11:40
    for any node that's being displayed.
  • 11:44
    One of the things that you'll probably notice in this node.tpl.php file
  • 11:49
    is the use of the render function,
  • 11:51
    and also, later on, you'll see the hide and show functions.
  • 11:55
    As we dive in and start looking at template files in Drupal,
  • 11:58
    one of the things that you'll need to learn about are renderable arrays.
  • 12:01
    From the theming perspective, renderable arrays are a mechanism
  • 12:05
    that allows Drupal to delay the rendering of any content to HTML for as long as possible.
  • 12:10
    This means that Drupal tends to pass around pieces of content
  • 12:14
    like a node or a block and all of the different things that make up that node
  • 12:17
    as a large structured array that at the very last moment gets rendered into HTML
  • 12:22
    by using the render function.
  • 12:24
    This makes sure that both modules and themes have a large amount of flexibility
  • 12:28
    when it comes to adding and removing things from the page.
  • 12:32
    In order to really understand why Drupal 7 uses this concept of renderable arrays,
  • 12:36
    I think it makes sense to step back for a second
  • 12:38
    and take a look at how things were done in Drupal 6.
  • 12:41
    In Drupal 6 you might have a node template file,
  • 12:44
    and in that node template file you have a simple variable named $content.
  • 12:48
    Your template file prints out that $content variable
  • 12:51
    which contains the body of the node and any additional CCK fields
  • 12:56
    or information that may have been added to the body of that node.
  • 12:59
    In our example here, you can see we're printing out 1 additional field on the page.
  • 13:05
    The problem with doing it this way is that if you wanted to manipulate the HTML
  • 13:10
    for that particular field or say, "Move that field to the bottom of the body,"
  • 13:15
    or "Place it somewhere else on the page," you would need to not print out the $content variable
  • 13:21
    inside of your node template file and instead print out each individual field
  • 13:25
    and the body of the node separately.
  • 13:28
    This works great until an administrator comes along and adds a new field to your content type.
  • 13:34
    Once they've done that, all of a sudden the field that they've added,
  • 13:38
    while it shows up in the form and administrators or editors can add content to it,
  • 13:42
    it won't actually show up on the page until the themer has come back
  • 13:45
    and added that field to the template file as well.
  • 13:48
    In order to get around this problem,
  • 13:51
    Drupal 7 introduced the renderable arrays.
  • 13:54
    Let's take another look at our node template file from Drupal 7
  • 13:57
    and see how we might make use of this renderable arrays in our theme.
  • 14:02
    In Drupal 7 that $content variable that used to be just a simple string
  • 14:08
    is now an array of all of the elements that make up the actual content of the page.
  • 14:14
    And now instead of just printing out the content variable,
  • 14:17
    we actually need to use the render function to wrap the variable
  • 14:21
    before printing it to the screen.
  • 14:23
    The render function will recurse through that content array
  • 14:26
    and take each of the elements inside of it and turn those into HTML.
  • 14:30
    The HTML is then returned, and we can print it out on the page.
  • 14:34
    In the example here, you can see that we're hiding the comments and links portion
  • 14:38
    of the content temporarily.
  • 14:41
    We're then calling print render content.
  • 14:43
    What this will do is take all of the pieces of that array, minus comments and links,
  • 14:48
    which we set to hide, and turn those into HTML and display them on the page.
  • 14:53
    Then later on in our template we go ahead and manually render the links portion
  • 14:58
    of the content variable and the comments portion of the content variable.
  • 15:03
    This gives us the flexibility to move individual elements outside of the main content
  • 15:08
    into different portions of the page or in the template file
  • 15:12
    but still render the entirety of the content variable.
  • 15:16
    This means that when an administrator comes and adds an additional field
  • 15:20
    to our content type in Drupal 7, that field will immediately start showing up where we've said
  • 15:25
    print render $content.
  • 15:28
    But we can still have control over individual fields and move those around
  • 15:32
    inside of our template file.
  • 15:35
    The important thing to remember here is the use of the render function.
  • 15:40
    There are a number of different variables inside of template files in Drupal 7
  • 15:44
    that require being wrapped in the render function in order to turn their content into HTML
  • 15:49
    before printing it out inside of your template file.
  • 15:53
    Another thing that we'll talk about is the use of dynamic template files inside of Drupal.
  • 15:58
    This is a really powerful system that allows you to do something like, say,
  • 16:02
    take the node template file, which is responsible for printing out all of the nodes on your site,
  • 16:07
    and say that for any node of the type article I'd like to use a different template file.
  • 16:14
    In that scenario let's say you have 4 different node types on your site
  • 16:18
    and you want all of them to use the node template file, except for 1.
  • 16:22
    You want that one to be slightly different, your article template file,
  • 16:24
    because it has tags and images that you'd like to display in a different place on the page
  • 16:28
    using some different markup.
  • 16:31
    You can create a dynamic template file using the naming convention shown:
  • 16:36
    node hyphen, hyphen the node type machine readable name--in this case article--
  • 16:41
    followed by the .tpl.php extension.
  • 16:46
    This is identical to how we did this in Drupal 6 as well,
  • 16:49
    which had this concept of dynamic template files.
  • 16:52
    The primary difference between Drupal 6 and Drupal 7 in this case
  • 16:55
    is that Drupal 7 uses 2 hyphens to separate the base name
  • 16:59
    from the more specific targeted name of your template file.
  • 17:03
    We'll also talk about where you can find all of these template files inside of Drupal
  • 17:08
    and how to override those in your theme.
  • 17:10
    Drupal, as we said, provides a set of default markup,
  • 17:13
    and it does so by providing default template files.
  • 17:16
    The user module, for example, provides a file named user-profile.tpl.php.
  • 17:22
    This contains the HTML markup for a user's profile page.
  • 17:26
    If you want to override that markup inside of your theme,
  • 17:30
    what you can do is copy that template file from Drupal core into your theme,
  • 17:35
    go ahead and make any changes to the copied version of the template file,
  • 17:38
    and then clear the cache so that Drupal finds your version of the template file
  • 17:42
    and starts making use of it.
  • 17:44
    It's important to remember to copy template files out of Drupal core into your theme
  • 17:48
    and not move them.
  • 17:50
    If you were to move the template file, it would no longer exist inside of Drupal core.
  • 17:54
    And if someone decided to switch to a different theme or disable your theme for some reason
  • 17:59
    and Drupal needed to use its default user profile template
  • 18:03
    and it no longer existed, this would cause errors on your site.
  • 18:07
    Drupal core provides a large number of template files,
  • 18:11
    and this list of template files will grow
  • 18:13
    as you add more and more modules to your Drupal site.
  • 18:16
    Every module likely outputs content in some form or another,
  • 18:19
    and when it does so, it probably does it using a template file.
  • 18:23
    Throughout the course of this series, we'll talk about overriding
  • 18:26
    and making use of a number of different template files,
  • 18:30
    some of the more common ones, like the page template file,
  • 18:32
    block template file, nodes and so forth.
  • 18:35
    We won't necessarily get into all of them, but it's good to know that these exist.
  • 18:40
    What we've seen in this presentation are a number of the concepts
  • 18:42
    that make up the first 2 steps in building a theme for Drupal:
  • 18:46
    step #1, info files, and step #2, template files.
  • 18:51
    Throughout the rest of this series, we'll be going through both of those steps
  • 18:54
    and all of these concepts in more depth.
  • 18:57
    We'll be overriding template files, creating info files,
  • 19:00
    and building out an entire Drupal theme as we go.

Introduction to Theming Basics for Drupal 7

Loading...

Theming Basics for Drupal 7 will provide a solid foundation for translating designs into Drupal themes. You'll learn to work with .info and tpl.php template files, how to add CSS and JavaScript, how to work with the render system new in Drupal 7, how to override templates, create regions, and use the Theme Developer tool. You'll start with the original HTML, CSS and JavaScript template files that were provided by the designer so that you can follow along in translating the design into a Drupal 7 theme. The video explains Drupal's design vernacular, concepts, and special needs. We'll show you how to associate the proper CSS & Javascript files, add all of the necessary regions, and control the HTML output through page and node-specific templates. You'll learn about the best tools and strategies for controlling the look and feel of your Drupal website. This video will cover the basics of Drupal theming while the more advanced theming topics of working with the template.php file will be covered in the Advanced Theming video. These videos pair with each other, and will finish the complete implementation of the 960 Robots theme used in both videos. Examples in this video are based on Drupal 7 and its variants. Here are the chapters included in this video;

  1. Introduction to Drupal 7 Theming
  2. Preparing a site for theming
  3. Enabling themes and theme settings
  4. Translating a design into a Drupal theme
  5. Helpful browser extensions for Drupal theming
  6. Creating the .info file
  7. Customizing the page template file
  8. Adding regions to the page template
  9. Customizing the node template
  10. Creating templates for node types
  11. Using the theme developer module
  12. Adding JavaScript to a theme
  13. Tour of popular page template variables
  14. Adding a screenshot to your theme
  15. Conclusion to Theming Basics

In this introduction video we set the stage for the Theming Basics series. We give an overview of what Drupal theming is, the common files we will work with, as well as the overall steps involved. Then we start off by defining and taking a look at the .info file and HTML template (tpl.php) files. We talk about regions, theme features, introduce the render() function, and explain dynamic templates. While we are teaching Drupal 7 theming, we also make sure to point out the important differences between Drupal 6 and 7.

This series will cover the basics of Drupal theming while the more advanced theming topics of working with the template.php file will be covered in the Advanced Theming for Drupal 7 series. These videos pair with each other, and will finish the complete implementation of the 960 Robots theme used in both videos. You can download the theme we're building from Drupal.org: 960 Robots.

Resources: 

Average review

4.333335
Average: 4.3 (3 votes)

Reviews

4

very nice and well explained. The best tuts for this purpose I've find in the net. great :)

Larry

4

As someone who has been mortified by the complexity of the Drupal Theme Layer, this video broke it down for me really well. It is so much more understandable to have an actual human explain things than peck it out of a wall of text. 

 

Now I need to know where that $content array comes from. I have full trust the Drupalize guys will tell me.

 

2 thumbs up!

5

I have been looking for a concise explanation of how to theme drupal from scratch and this is it.

Thanks!