I am a designer and a newcomer to Drupal who is interested in learning custom themes from scratch and possibly creating an infographic that summarizes the logic of the various custom theming strategies in Drupal for a typical site build process. I've read several books looking to specifically learn how to build custom themes and I believe I have reached the point where I have a pretty good understanding of creating and implementing new regions in the .tpl files, theme functions, template arguments/suggestions, pre/process functions, the render api's alter hooks and css management but I'm a little overwhelmed by all the options and I have been trying to look for or develop the logic for using one strategy over another in the different areas of the site build process.
The best book that I've found so far is the aptly titled "The Definitive Guide to Drupal 7," which floored me with its comprehensive and groundbreaking coverage of Default Regions, Hidden Regions, Module-Specific Regions, and when to use regions vs. hard-coding variables. Overall it does a fantastic job of covering all the individual theming techniques showing the syntax, and then walking through an exercise. My only issue after studying the material within the book, which I admit might be more of a personal issue than something everybody experiences, especially given that each exercise implies to a varying extent the particular purpose of each theming technique, is an all-inclusive comparative analysis or custom site build demonstration that reveals the logic of using one theming technique over another in different areas throughout an entire site build process. My new goal than is to look for or develop the logic of using one strategy over another in the different areas of the site build process and to create an infographic for future designers to use while they learn Drupal. Probably as a flowchart from the perspective of a traditional site build process with an accompanying definition list to be used in tandem with a book like "The Definitive Guide to Drupal 7".
In the book, "The Definitive Guide to Drupal 7," an all-inclusive comparative context of all of these techniques is hinted at on page 323 which states, "Use templates and theme functions to modify markup and alter hooks to modify contents, structure, or placement of elements before they're rendered. Furthermore on page 353 it returns to this comparative context by warning against relying too much on template overrides, arguments and suggestions and offers some useful guidelines in determining what method to use such as using preprocess functions to handle "one-offs", using theme hook suggestions when the target is very specific and narrow, and finally using CSS class arrays to add basic css hooks in the markup.
So, from what I understand template files and theme functions are used to override HTML markup. But whereas template files are usually overridden in order to make major changes to the structure of the markup of a page or to implement more or fewer regions, theme functions should be used in order to make smaller changes such as adding form elements.
Theme hooks merely refers to the naming convention for a declaration of a template/theme function's implementation, such as node.tpl.php and theme_node(), whereas a theme function refers to the code to be executed(?). Theme hook suggestions are more specific then the default template's and theme function's and are used when you want to make changes to a particular page(s), block(s), or theme function. I'm still talking about changes to HTML markup: template overrides=big changes, theme functions=small changes, theme hook suggestions=the ability to progressively traverse drupal's template/theme function/block specificity (theme hook suggestions for blocks=content specific changes). So, with theme hook suggestions you could target a specific template/theme function/block or a set of specific templates/theme functions/blocks for narrowly-focused customization (to the HTML markup)
Pre/process functions are similar to theme hooks in that they allow you to intercept and alter how a page is rendered without having to override a template file but pre/process functions deal with adding, removing, and modifying variables (p 314). What possible scenarios would you use this for? The book gives examples on dynamically adding CSS classes to variables, hiding variables in certain circumstances, and adding variables in certain circumstances.
Lastly, as explained on page 323, the render API's alter hooks are similar to theme hooks because it is used to generate HTML, but alter hooks are used to modify contents, structure, and placement of HTML elements before the page is rendered. Examples in the book use alter hooks to dynamically add HTML for new bit of content into a region, moving content from one region to another based on certain circumstances, and changing the contents of tabs based on certain circumstances. Here, the alter hooks do seem to have a similar purpose to the views or panels modules, except more complicated...
Comments
Hi @indigital
I'm the author of the theming chapters you mention, and it seems like you've grasped the concepts of each pretty well. I think it's awesome that you are looking to create an infographic on this stuff. We could all benefit from such a document.
I've seen similar feedback in reviews of the book, so I'd like to take this opportunity to explain some of this.
This is actually a problem with Drupal theming itself, and a very common complaint. There are WAY too many options and unfortunately there is no hard and fast rule for when to use one technique over another. The inconsistencies make things hard and this is an area where most of us really want to see changes in Drupal core. The choice for which technique to implement lies in the hands of module developers 90% of the time, so knowing how something is being built is the main key to changing it. This is why being able to identify the technique used and knowing the foundation of each is critical.
Like I said, I think you got quite a bit out of the chapters, and would love to see what you come up with. I think it's a very worthwhile cause, and can help us identify concrete areas to improve in Drupal 8.
Hi Jacine
Hey, thanks for writing! The Definitve Guide to Drupal 7 and the chapters that you authored are without a doubt, the most thorough and concise explanation of Drupal and custom theming out there. I completely understand your points, its interesting that the issue of choosing one theming technique over another is a common problem in Drupal. I'm still learning Drupal and will have to sift through all the information that I've collected but I will definitely post my progress on the infographic to confirm and elaborate on the information I gather. Having something that would at least lay out the various options and list the pros and cons would serve as a great springboard to begin theming in Drupal and to develop well-informed personal preferences.
Welcome :)
A consequence of open source is that some features come into existence piece-wise and evolve over time without original comprehensive plans or constitution. Some APIs are useful, but others sometimes just feel like responses to old workarounds.
As Jacine mentioned, this has resulted in a raffish cornucopia of theming APIs that are confusing not just for beginners but for seasoned vetrans as well. Drupal 7 theming is much more difficult than Drupal 6. I experience much more iteration and debate of technique.
Personally, I think we need better tools and fewer gagets. Themers want a chef's knife and we given them a Slap Chop.
What's good is that we're talking about it. What's better is that this is open source. We can fix it. If you have ideas, please contribute to discussion on g.d.o and other channels. Whether newcomer or vetran, we're all authors of the future of Drupal theming.
Shameless plug
Regarding your questions on preprocessors: If you haven't yet, please check out some Drupalcon videos of a session I've given:
http://www.archive.org/details/ThemePreprocessFunctionsAnIntroduction
http://www.archive.org/details/ThemePreprocessFunctionsAnIntroduction_383
Hello there, Brandon! I am
Hello there, Brandon!
I am also a huge fan of that huge book :) I'm writing training materials on this very topic right now, I would love to see an infographic to direct learners to.
I wonder about your target audience.
Group A. You say they are designers? That is, visual designers who need to understand Drupal to create visual mockups for it?
Group B. Or are they coding designer/developers who are interpreting a visual design in Drupal?
For both groups who are NEW to Drupal, I would caution against bringing them to the VIP template.php room first, doing lines of PHP, and getting lost in preprocessing, and overriding theme functions.
The best practice is to take a "configuration before coding" approach. They should look closely at configuring Views to get the markup you want, configuring content types and field display, and configuring any other contrib modules. That is, before making theme-specific alterations. This configuration before coding approach is one our professional services team at work promote to our clients time and time again. With that, they see less errors and less trouble.
A good method is to "theme with the markup you have, not the markup you wish you had". (- Laura Scott) If you are talking to markup purists about Drupal, they will be quickly frustrated at this stage. There is quite a lot you can do with CSS overrides.
Best practice with module development implies that module developers should output template suggestions and theme template files. This allows themers to override markup by duplicating the file in their own theme folder. This is the ideal situation. Yet of course, you want to avoid template-itis.
Template files should manipulate markup on a macro scale, and preprocess functions should alter smaller chunks of content, such as overriding the output of a variable.
Common pitfalls include:
- Creating preprocess functions for something which can be configured in the UI, or using String overrides, etc.
- Template-itis, making themes with many many .tpl.php file overrides, with often just slight alterations.
- Manipulating content layout in the theme layer, when there are superior modules such as display suite, panels/context or others.
For Group A- They may be used to being able to design every last element of the site. Especially if they are new to CMSs. Drupal comes with quite a bit out of the box. Error messages, tabs, user registration pages, which designers often forget. Try the Style guide module to show what you may have forgotten. http://drupal.org/project/styleguide
For Group B- they may be used to tag based template systems like EE and WP. They can create their perfect markup and pop in their "tags" which output results from their CMS. Drupal is the furthest you can get from that. I think a better metaphor would be an "extruder". You have these patterns and shapes and your site is extruded through it.
The difference with Drupal is what I call the "markup middle man". The historical aim of Drupal is to put powerful tools like content type creation and query building (Views) in the hands of trusted line managers and site builders. Whereas in other systems you actually have to get at the templating layer to alter queries (give me X posts of Y category), in Drupal, we use Views (in most situations).
Recapping:
- Configuration before coding
- Theme with the markup you have
- Less is better, more maintainable and sustainable.
I do believe a little bit of knowledge is dangerous. Inexperienced developers can do some damage, but experienced PHP developers can do the worst damage when they are just getting to know Drupal. They can engineer entire parts of Drupal on top of Drupal. Always emphasize they should learn site building first and coding later.
Wiser words
Wiser words have never been said.
I also recommend looking at
I also recommend looking at this presentation by Tim Cosgrove from Treehouse Agency:
http://treehouseagency.com/blog/tim-cosgrove/2011/10/22/modern-theming-b...
And watch the recorded presentation from BadCamp
http://treehouseagency.com/blog/taran-rampersad/2011/12/06/badcamp-video...
It's sort of cut off, so you can watch it along the same time.
Hello
Hi Heather,
Thanks for the leads and for the great advice. Even though I've read several books on Drupal I came into it wanting to learn how to build a custom theme from scratch so my full attention has been limited to custom theming and only tangentially on configuration, but I see your point about "opening up the hood" before fully understanding the power of configuring a site using views and other such modules. When developing an infographic I will definitely try to incorporate your links and advice as a guide in the process so it can be a model of best practices. As a newbie, I'm in need of a comprehensive guide that breaks down and sums up Drupal site development as much as anybody else. Right now I'm developing a list of theming options categorized by areas of a site build and ordered by a "scalpel," "chisel," and "hammer." The goal is to include with each entry a brief description and common uses relative to the site area of the category. As Jacine pointed out, there is going to be a lot of duplication and overlap, which is my original grievance with Drupal, but I hope an infographic will help me to organize my thoughts and to develop well-informed personal preferences.
Thesis of Custom Theming Infographic for Community Review
Here is the content that I've gathered on Custom Theming using drupal.org and the books "The Definitive Guide to Drupal 7," by Melancon et al and to a lesser extent, "Beginning Drupal 7" by Todd Tomlinson. As I said before I'm still learning Drupal and began compiling this information as a study guide rather than as an knowledgeable expert in the field. I'm really hoping that the drupal community will help to evaluate and elaborate on the information, particularly in the examples of common/useful/practical applications of each theming technique, right now the examples are "limited" by what was covered in the books.
I've ordered the list and list items from basic theming strategies to major/advanced theming strategies. Starting with Basic Page and Article following the advice that Heather offered concerning the need for an awareness of the potentials of font-end configuration. However, even though I did watch the link to Tim Cosgrove's BadCamp presentation that Heather recommended, and was really impressed and excited, as a newbie that tends to become easily perplexed, I still don't quite understand the theming strategies that were discussed in the presentation so there is nothing here on the Bean, Fieldgroup, or Display Suite modules. Maybe someone could point me to a link or book that offers a real simple and straightforward explanation of those modules and/or add them in this list format:
Modifying Contents, Structure, and Placement of Elements
*Views can be used with panel panes to define what content should be rendered, how many items should be included in the list, and how those items should be rendered. Using Taxonomy, content authors select the appropriate taxonomy term that is associated with each panel pane, and the content automatically appears in the right spot on the right page.
**Render Arrays are structered arrays that contain nested data and other information that drupal uses to render HTML (p 321) they are similar to theme hooks in that they use template and theme functions but they are modified using alter hooks (323).
***It’s best to do these operations in preprocess or process functions in order to keep your templates clean and more manageable (329).
Styling
*Enabling aggregation and compression for CSS files (admin/config/development/performance) on all live sites is highly recommended, as it will speed up page loads quite a bit (341).
Adding, Removing and Modifying Variables
*use devel module’s dpm() function to determine all the available variables to work within each theme hook p.311
** These functions are run prior to the template being displayed, thereby allowing you to manipulate the variables for use in the templates themselves.
Modifying HTML Markup
*These are theme specific, meaning if the site employs more than one theme it will not apply.
** The block.tpl.php file, for example, is used when a block is being rendered. Template files can be overridden by copying them into your own theme. It is also possible to make specific overrides with naming conventions, such as block–user.tpl.php.
*** Regions play a large part in defining the high-level structure of HTML markup. Typical regions include the header, footer, sidebars, and content, most of a site’s content is output inside these regions.
Theming Strategies in Context now a Wiki page
I have created a wiki page called "Theming Strategies in Context" (http://groups.drupal.org/node/220129) so anyone who is interested in elaborating, adding, or improving the information contained in the chart posted in this discussion, you can now do so there.
Thanks