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).