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
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
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
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
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
It is very time consuming and error prone to repeat "click steps" among multiple sandboxes, development and production in order to get your new image styles to appear. In this video, I will show you how to write code that turns your "custom" image style into a "default" image style so that you can use source control to update all of your site environments.
I am assuming you already know how to create image styles in the administrative interface. Familiarity with basic module development is helpful, but if you follow the techniques and patterns I demonstrate, you will successfully export your image styles into a new custom module.
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
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
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
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
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
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
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
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
PHP for Themers
FreeBefore 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
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
This screencast introduces the awesome Views Bulk Operations (VBO) module, that can be used to perform actions on objects listed with Views. The screencast covers:
- How to add VBO fields to your views, to allow actions
- That these fields are displayed as check boxes (or possibly radio buttons) in your view
- That each VBO field is tied to one type of data in the view (such as nodes, node revisions or users)
- That some VBO actions have configuration pages
Additional resources
Views Bulk Operations (VBO) guide (Drupal.org)
This screencast shows how to repeat scheduled events – in this case sending repeated reminders to users who have not logged in for a month. The screencast covers:
- How to set up a component for scheduling (as seen before)
- The point of re-using an ID for scheduled tasks, to avoid duplicate reminders
- The trick of finding a good triggering event for scheduling (which is not always easy)
- The trick of having a component schedule itself upon completion, thereby repeating the scheduling
- How to delete scheduled tasks, and why that may be a good idea
Additional resources
Rules guide (Drupal.org)
This screencast shows how to use fields when scheduling Rules components. In the example we are also using the Flag module, to allow people to sign up for reminder e-mails 24 hours before an event starts. The screencast covers:
- Using flags to load user lists in Rules
- Scheduling a rules component from a triggered rule
- Some words about rescheduling the evaluation if the event date should change
- Using conditions to make fields available in Rules configuration
- Using a date field to set evaluation time
- Adding an offset to the evaluation (-1 day)
- Verifying the configuration by executing the action set manually
Note: It would have been smarter to use the “Before saving content” event for this rule – to make it act on both new nodes and updates of existing ones. If combined with a check to see that the event is in the future, you would actually have pretty good notification system!
Additional resources
Rules guide (Drupal.org)