1

Considering that some modules implement hook_process() to alter the content of some variables and replace their content with themed content, in which cases should a module implement hook_process() instead of hook_preprocess() (or hook_preprocess_HOOK())?

function rdf_process(&$variables, $hook) {
  // Handles attributes needed for content not covered by title, content,
  // and field items. It does this by adjusting the variable sent to the
  // template so that the template doesn't have to worry about it. See
  // theme_rdf_template_variable_wrapper().
  if (!empty($variables['rdf_template_variable_attributes_array'])) {
    foreach ($variables['rdf_template_variable_attributes_array'] as $variable_name => $attributes) {
      $context = array(
        'hook' => $hook, 
        'variable_name' => $variable_name, 
        'variables' => $variables,
      );
      $variables[$variable_name] = theme('rdf_template_variable_wrapper', array('content' => $variables[$variable_name], 'attributes' => $attributes, 'context' => $context));
    }
  }
  // Handles additional attributes about a template entity that for RDF parsing
  // reasons, can't be placed into that template's $attributes variable. This
  // is "meta" information that is related to particular content, so render it
  // close to that content.
  if (!empty($variables['rdf_metadata_attributes_array'])) {
    if (!isset($variables['content']['#prefix'])) {
      $variables['content']['#prefix'] = '';
    }
    $variables['content']['#prefix'] = theme('rdf_metadata', array('metadata' => $variables['rdf_metadata_attributes_array'])) . $variables['content']['#prefix'];
  }
}
3

I'll try to stay away from explaining the whole theme system. The order and use of the functions is best explained here.

Preprocessor functions run before process functions. You should run hook process for cases where you want to affect every theme call after all the preprocessors have run. (Well, I believe it is only every theme call with a .tpl file.)

RDF is a good example as it wants to insert meta data into every theme template. But in most cases it is not something you would want to do. Another example would be something like the Pirate module.

Preprocessors are more targeted. If you only want to change lists for example you could use a preprocess hook there. I believe that hook_preprocess_HOOK()s are run for templates and functions.

hook_preprocess() can be used in a theme if you wish to preprocess something which is not overridden in your theme, as the theme_preprocess_HOOK() won't trigger unless you do.

| improve this answer | |
  • 2
    All correct except that in D7, preprocess/process functions are called for theme functions too, not only templates. – Berdir Mar 24 '11 at 19:17
  • Are you sure? The docs say otherwise "If the implementation is a function, only the theme-hook-specific preprocess and process functions (the ones ending in _HOOK) are called from the list above." – Jeremy French Mar 25 '11 at 9:54
  • Oh, that is possible, yes. I misread that in your answer. – Berdir Mar 25 '11 at 10:46

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.