Free Website Tips
Recent Twitter Posts
DrupalCon Cliff's Notes: Theme Preprocess Functions - An Introduction
This is working on the theme layer - specifically preprocessing. Theme pre-processing exists for when the default Drupal output isn't sufficient. It answers the question, "how can we create more useful content and contextualized it (read: CSS classes)?" Preprocessing stands between the module and theme layer.
dd() is an equivalent to dpm() that instead dumps to a file in your temp directory which is good for production sites
Theme Preprocess Capabilities (aka common use cases)
- Define new variables
- Specify the template to use (~25min)
- check the theme_hook_suggestions with dpm in the passed variables to see what can exactly be defined as well as define new ones
- $vars['theme_hook_suggestions'] = 'node__special';
- This allows you to create a new node--special.tpl.php that gets picked up based on the conditions you set to trigger adding this in template.php
- Contextual CSS
- menu_get_object() returns currently viewed node from page function
- Add class based on node taxonomy term, then page has new class that can be themed against
Unfortunately, at 41 minutes, 48 seconds the cord to the projector got loose. For those in the session it wasn't an issue when he plugged back in, however for the rest of the video recording there is a blue screen (of death :).
That is quite unfortunate since it was really getting into the meat of things right about then. Fortunately, this isn't the first time Carl's given this talk. I headed over to DrupalCamp Colorado's recording of his same talk. Downside of that one is the video seemed pretty choppy and you can't see the screen (though it was fun to see Carl!).
- Every template has a preprocessor [slide 32]. The format of the preprocessor function for a template file like file-name.tpl.php is theme_preprocess_file_name() [slide 33].
- Tangent: Drush has a command to reset a user's password (drush upwd admin --password="newpassword") or provide the forgot password link for admin (drush uli) - check out http://randyfay.com/node/92
- html.tpl.php is the entire shell returned
- Why use preprocessing instead of Display Suite or Omega with Context, or Panels, or...? This is for the control freaks who want to interact with Drupal from a code perspective. Also, the preprocessor layer has been around a lot longer and can sometimes do things UI-level modules don't have the flexibility to do.
- dpm() - from the devel module - will sometimes render on the next screen if drupal messages is called before dpm was in the page load. Also, be careful you don't overload dpm() - it is possible.
- Clear the cache whe adding tpl files and adding functions to template.php - otherwise your changes won't be picked up by Drupal.
- Use check_markup() and check_plain() on data that may be unsafe (ie user entered)
- In Drupal 7, we now have field_get_items() which helps when there are multiple values.