Advanced Theming for Drupal 7

This page is archived

We're keeping this page up as a courtesy to folks who may need to refer to old instructions. We don't plan to update this page.

Alternate resources

This Advanced Theming in Drupal 7 series picks up where the Theming Basics with Drupal 7 left off. This series will cover the new page rendering system, and shows you the process of iterating through the theming process with the use of various debugging statements and the theme developer tool. You'll get a crash course through PHP so that you can start using advanced theming concepts such as: how to theme forms, how to cleanly expose information to the theme system, and how to empower your site administrators by making certain parts of the theme available to them for customization. You'll also learn more about some of the new theming hooks that are new in Drupal 7. By the end of the video, you'll have enough tools to be able to completely control the HTML, CSS and JavaScript that Drupal outputs. You can download the theme we're building from Drupal.org: 960 Robots.

Tutorials in this course
More information

In this series, we talk about template.php, the file where all your functions live. We’ll also discuss preprocess functions and overriding theming functions. You’ll learn how to create new variables and override existing ones using template.php. With these tools you can allow your theme to have complete control of outputted HTML.

In this video you’ll learn when to use a tpl.php file or your template.php file, and we’ll look at differences between them. We’ll also review the three main steps of theming.

Additional resources

960 Robots

Categories
Drupal 7
More information

Before we dive into advanced theming we are going to review the basic PHP you will need to use. This is a short review of PHP, including topics like data structures (arrays and objects), conditionals and operators, loops, and functions.

Additional resources

960 Robots

More information

In this video we introduce the template.php file and create our first preprocess function. We explain what the preprocess hooks are and how to name the function properly, then we show a list of existing variables, and how to modify one of those existing variables.

Additional resources

960 Robots

More information

Continuing on our work with preprocess functions, we look at how to add a new variable for our template files to use. We also see how to change the output based on whether the user is logged in or not, and how to add a variable to the t() function.

Additional resources

960 Robots

More information

We'll go a step further with our preprocess functions and look at working with node variables, and how to limit new variables to only specific content types.

Additional resources

960 Robots

More information

In this video we will look at how to add CSS and JavaScript to our site using drupal_add_css() and drupal_add_js(). We will also show using these functions to conditionally add the CSS and JavaScript, within our page preprocess function.

Additional resources

960 Robots

More information

In this video we'll see how we can create our own template suggestions. We'll do this in our node preprocess function to create a new node template that will be used depending on the day of the week.

Additional resources

960 Robots

More information

In this video we will look at overriding a theme function instead of working with the template files. We will be modifying the breadcrumb using theme_breadcrumb(). to do this we'll use the Theme Developer module to help us find what we're looking for, and then we'll walk through how to override.

Additional resources

Theme Developer module (Drupal.org)
960 Robots

More information

In this video we will see how we can create our own preprocess function for a theme function (not just templates). We're going to use this to display the user's full name on the site wherever the the username would normally appear. We start by creating a full name field, and then show how to get this working on our site.

Additional resources

960 Robots

Categories
Drupal 7
More information

In this video we cover a new Drupal 7 feature which lets us create suggestions for theme functions, in addition to suggestions for templates. We'll use this to modify the tags displaying on a node in our site.

Notes:
In the example: $item['#options]['attributes'] += $variables['item_attributes_array'][$delta]; += is a shortcut for array_merge. So it is ensuring the contents of $variables['item_attributes_array'][$delta] are included in $item['#options]['attributes'] so that any class, rdf info or other html attributes are appropriate applied to the tag links. It's possible for any number of modules to set HTML attributes on the tags field in the theme we need to make sure that they get printed out appropriately.

Additional resources

960 Robots

More information

There are four alter hooks that are now available to the theme layer in Drupal 7. This chapter walks through two of those hooks, hook_css_alter() and hook_js_alter(). These hooks allow the theme layer to reorder, delete or replace specific CSS or JavaScript files that are being loaded by Drupal core or other contributed modules.

Additional resources

960 Robots

More information

This chapter explains hook_page_alter(), which is another alter hook new to Drupal 7 and to the theme layer. Each page rendered by Drupal is contained within an array where the theme is able to go in and rearrange, delete or duplicate specific page elements.

Additional resources

960 Robots

More information

This chapter gives an introduction to how you work with Drupal's renderable, form arrays by adding an image button to the search form. It also walks through the process of adding the necessary classes to a form element so that the image button is properly aligned. The Forms API reference document is a pretty essential reference document for creating and editing forms in Drupal.

Although we use the function dsm() in this video, this should probably be dpm() as that's the preferred function name. The functions dsm() and dpm() are identical.

Additional resources

960 Robots

More information

The ability of having alter hooks within the theme layer starts to blur the line between the logic and presentation, and so there are a few things to be aware of when using hook_form_alter() from the theme layer. The biggest thing to know is that the form rendering process has already gone through the creation and validation preparation process at the module layer, and so you shouldn't be drastically changing the functionality of a form at the theme layer. This form alter hook at the theme layer is primarily for aesthetic clean-up, styling and additional design. This chapter gives some specific examples of things to watch out for.

Additional resources

960 Robots

Categories
Drupal 7
More information

This chapter goes through the process of adding a specific form id to the theme registry with the hook_theme() function, which allows the creation of a new function that targets a specific form. Specifically in this chapter, comment_form is added to the theme registry so that we can create a ninesixtyrobots_comment_form() function in order to add a column to the comment form so that the name and subject fields are displayed side-by-side.

Additional resources

960 Robots

Categories
Drupal 7
More information

This lesson goes through the process of creating Theme Settings so that administrators of a theme are able to turn on and off specific features within the the theme, as well as specify other relevant settings.

Note: Twitter has recently shut down their v1 REST API so this lesson's site_slogan adjustment no longer works (it will always return no values because there is no API data found). The core concepts of how to create theme settings are still correct, you just won't be able to see the output from Twitter.

Additional resources

960 Robots

More information

This chapter shows some general best practices for keeping your template.php file clean as well as making it easier for designers to work primarily with the template files when possible to avoid them having to make too many changes to template.php. Specifically, some HTML classes are removed from the ninesixtyrobots_comment_form() function in template.php and moved to the comment-form.tpl.php template file, which is specified as an option in the ninesixtyrobots_theme() function. The rendering logic is also removed from the theme function and into a preprocess function so that it can be passed in as a variable to the new template file.

Additional resources

960 Robots