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)
This screencast shows how to start using Rules Scheduler. It does this by mimicking the Comment Closer module – closing the comments on articles two weeks after they are created. The screencast covers:
- Rules Scheduler needs components – you can only schedule prepared components
- Components can be scheduled as actions, for example from reaction rules
- The evaluation time for scheduled tasks is set with strtotime(), which means that you can use highly flexible expressions
- You can also use data selection, combined with offsets if you want to
- Every scheduled task should have a unique task ID, or it will replace existing tasks
- Scheduled tasks are displayed under the Schedule tab on the Rules admin pages
- You can delete tasks manually if you want to
- You can schedule tasks manually, without the need of reaction rules
- It is sometimes useful to execute rule components manually, to see that the scheduled tasks will work as expected
Additional resources
Rules guide (Drupal.org)
This screencast shows how to go about debugging Rules configuration. It covers:
- Enabling the debug information in Rules, showing rules evaluation.
- How to drill down into Rules’ debug messages.
- That most (or all) times, it is your configuration and not the Rules engine that has the bug.
- That the most common bug is condition that evaluates in an unexpected way.
- How to see how long time rules execution takes – and why/when this may be important.
- How to use the debugging action in Rules, to inspect the data in Rules.
Additional resources
Rules guide (Drupal.org)
This screencast shows how to work with export and import of Rules configuration, in particular using the Features module. It covers:
- Why you should use the Features and Strongarm modules.
- How to export Rules configuration with Features.
- How to import Feature-exported Rules configuration to a site.
- What reverting rules configuration means, and how to do it from Rules or Features.
- How to (eventually) export Rules configuration manually, and some words about the high readability in the Rules export code.
- How to import manually exported Rules configuration, including some available options/tweaks.
- Why configuration management is an important issue for Drupal.
For more information and tutorials on the Features module, check out the Drupal Deployment with Features & Drush Series.
Additional resources
Rules guide (Drupal.org)
This screencast presents a way to automatically create the article promotions used in the previous screencast. Topics covered are:
- Using the after new content has been created event, which does provide content NID
- Working with rule set components.
- Creating new entities with Rules.
- Setting field values in new entities – even complex fields like images.
- Force-saving entities, for example to get node IDs.
Additional resources
Rules guide (Drupal.org)
This screencast presents a way to help manage article promotions, separate content types used for promoting articles, providing more flexibility than standard teasers. The screencast shows how to have promotion nodes being published, unpublished and deleted in sync with the articles they belong to. Topics covered are:
- Reacting on the node deletion event.
- Using fetch entity by property to load a list of all relevant promotion nodes.
- How to have the loaded list unlimited in length.
- Using the delete entity action.
- How to call rule components from a reaction rule.
- Why it might be a good idea to have actions in separate components when acting on node updates.
- Cloning rule components.
- Publishing and unpublishing nodes.
Additional resources
Rules guide (Drupal.org)