6

I'm trying to get my head around what these are. I've read a few articles and posts introducing Drupal theming where these are mentioned, but I'm struggling to understand what they are and what they do - each explanation I've seen so far (including on Drupal.org) makes an assumption that I know PHP or have programming experience.

Could someone explain in layman's terms or point to a good, simple-to-understand explanation, please?

  • 1
    Have you reviewed this drupal node? It's very comprehensive: drupal.org/node/341628 – chrisjlee Sep 29 '11 at 4:20
  • @chrisjlee The above node even says "If you are not already familiar with PHP, the process might seem somewhat intimidating, but it generally does not require that you understand anything about actually writing PHP." – mbomb007 Jan 11 at 19:53
11

Well, the theme layer is PHP, so you will need a minimal understanding of some programming concepts (e.g. what a function is). Especially since you will have to write some basic PHP within them. So the following might not be perfectly suited for a non programmer initally, but I tried to link everything so you can read more about it. Also, add comments if something is not clear.

The theme layer builds upon naming conventions on how to name your functions to have them called automatically. Similar to the so called hooks. At least preprocess functions are a special variant of hooks. I'll come back to that later.

Any module can register theme functions (or templates) by using hook_theme() (You don't need to understand how that works right now). ) For example, it can basically tell Drupal that it has a theme function called mymodule_fancything. This module is then also responsible for providing the default theme function, which is prefixed with theme_. In the previous example, that module needs to define the function theme_mymodule_fancything.

Later on, that, or any other, module can call that theme function with the function theme() like this: theme('mymodule_fancything', $arg1, $arg2); (D6) or theme('mymodule_fancything, array('arg1' => $arg1, 'arg2' => $arg2));` (D7).

Then, that function will figure out which function to call. First, it will check if there is a theme override for the active theme. That means, a function that is prefixed with themename_ instead of theme_. So in our example, themename_mymodule_fancything. If it finds that function it will call that and if not, it falls back to theme_mymodule_fancything.

As you've seen in the example code, you usually pass a number of arguments to a theme function or a template which contains the data that should be displayed. Sometimes you want to further process/change that information before it's passed to the theme function or the template. Or a module wants to change something of another module. Also because theme functions/templates should not contain any complex PHP processing. Unlike with the mentioned hooks (which can be implemented by all modules and all are called), a theme function can only by overridden once.

That is where preprocess functions come into play. They are invoked by the theme() and receive all arguments passed to that theme function (only in D7) or template. Then they can be altered/extended in any way. There is a large number of rules what preprocess functions are called when but basically, you usually only use one that is specific for a given theme function/template. That looks like this: name_preprocess_hook(&$vars). name can be the name of a module or theme and hook is the name of the template/theme function. There are tons of examples for preprocess functions in core, search for preprocess.

Remember to rebuild theme registry (e.g. with devel.module or by re-saving the theme configuration form).

  • crikey... I think I'm going to need to cover a rudimentary understanding of PHP to get my head around your explanation. The concepts involved are just too nebulous and complex for me to understand, unfortunately. I've been trying to find a way to learn PHP as part of a Drupal project, but it looks increasingly likely that I'll have to learn PHP first, separately, before moving on to understanding Drupal theming a bit more. Thanks for your in-depth explanation, anyway, Berdir - much appreciated :) – James Jul 15 '11 at 21:16
  • 4
    I'm aware that it might not be easy to understand without knowing PHP. But the thing is, as mentioned above, this is PHP. Even if you understand the basic concept (which is not that complicated), withing those functions, you will write PHP. No way around that. So you need to understand a bit PHP (basic stuff like accessing/printing variables, conditions and loops, array/object structures and how to access them) anyway. – Berdir Jul 16 '11 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.