Skip to content
Mehdi Kababfrontend / author
⁠The notebook

The agonies of Drupal 7 theming

In Drupal 7, hiding a field and injecting its CSS don’t happen in the same place. A look back at theming that goes round in circles.

Drupal 7’s theming system is a mess. Jacine Luisi, co-author of the book ⁠The Definitive Guide to Drupal 7, sums up exactly what I think in her article Let’s Do Something About Drupal’s Theme System. Rather than rewriting her article here, I’ll show you what it’s all about.

Why is Drupal theming so complicated?

Ever since I started working on Drupal 7, not a week goes by without me cursing its theme system. As Jacine points out, the many entry points and the multiple levels of interaction often make it hard to find where to step in.

Which preprocess do I call? This one?
Lost. Let’s try another one, or a hook_alter.
Still nothing. Maybe in a hook_process?
In the end I have to override a hook_theme because I can’t act on this pre-render…

I’m exaggerating a bit. Then again, not that much.
You’re going to tell me that with Drupal 6 we were already used to this mental gymnastics, to which I answer yes, but. But it was less anarchic. Example.

A very simple example

For one specific content type, a client asks to be able to freely insert JavaScript code and Cascading Style Sheets (CSS) styles from the contribution form. No problem: I just need to grab the content of the fields and attach it to the page with the drupal_add_js and drupal_add_css functions, using an inline type and the right scopes. No sooner said than done, I do it in an implementation of hook_preprocess_node() and, unsurprisingly, it works.

Note

The code snippets below are simplified to focus on the topic.

php
<?php
/**
 * @file template.php
 */
/**
 * Implements hook_preprocess_node().
 */
function MYTHEME_preprocess_node(&$vars) {
  // We use the `value` property instead of `safe_value`, which encodes quotes.
  if (!empty($vars['field_inline_css'][0]['value'])) {
    drupal_add_css($vars['field_inline_css'][0]['value'], array(
      'type'       => 'inline',
      'group'      => CSS_THEME,
      'every_page' => false,
      'media'      => 'all',
      'preprocess' => false, // do not aggregate with the other styles
    ));
  }
  // -- snip --
}

Unfortunately the HyperText Markup Language (HTML) markup of the fields is still generated in the content flow. Fine, I just need to hide them (in the Drupal 7 sense of the term). I look into applying a hide() on my fields from my preprocess_node implementation. Damn, they’re already marked as rendered, their content already computed, etc. No better from a hook_page_alter(), nor from the dedicated template file. Yet the official documentation never stops showing how easy it is to manage field visibility.

So what now? Should I waste time rendering each field to display for this content type one by one, or push the investigation a bit further? I go for the second option, the situation seems that absurd to me.

Quickly, I find that I can hide my fields by implementing the hook_node_view_alter(). Everything works.

php
<?php
/**
 * @file template.php
 */
/**
 * Implements hook_node_view_alter().
 */
function MYTHEME_node_view_alter(&$build) {
  hide($build['field_inline_css']);
  hide($build['field_inline_js']);
}

However, handling these fields in two places isn’t very readable. So I move the code I had added to the hook_preprocess_node() implementation into my hook_node_view_alter.
Fiasco: only the JavaScript injection is taken into account. The CSS styles are simply ignored ;!

What The F*** (WTF) Drupal (╯°□°)╯︵ ┻━┻

And that is how I resigned myself to splitting this theming step into 2 separate places. All of it with explanations in comments, so that I don’t get lost in them in a few months, or simply to help my colleagues find their way around.

How did we get here?

The blame lies partly with an unnormalized data structure, coupled with a render array system that is tempting but doesn’t keep its promises. Every module handles the data it passes to the render Application Programming Interface (API) as it pleases. As a result there are almost as many cases as there are modules :-/ In the template files, we get a soup of simply typed variables and render arrays.

Also, modules can change the structure of render arrays and throw spanners in the works for theming (the Field Group and Display Suite modules are good examples).

Finally, many complex situations could be avoided if Drupal’s core systematically used render arrays. There always comes a moment when, while trying to change the #prefix of an element, we run into pure HTML. All of that because an implementation of hook_theme() doesn’t return a render array, like theme_links() for example…

The future of theming with Drupal 8

FIXME. That’s the idea :D
Jacine proposes a standardization of components which will simplify templating by streamlining how a page is built and, above all, each module will be able to stop drowning us in miles of CSS classes “just in case…”.