2

I'm going to explain my problem through the use of pseudo code. Right now, i have in template.php the following code

function THEME_process_page(&$variables)
{
    if($variables->view == 'calendar')
    {
        // do something
    }
    else if ($variables->view == 'google_maps')
    {
        // do something
    }
    else if($variables->content_type == 'news')
    {
        // do something
    }
    else if($variables->content_type == 'event')
    {
        // do something
    }
}

Instead of writing a whole bunch of if statements for each view and content_type, can I do something like

function THEME_process_view_calendar()
{
    // do something
}
function THEME_process_view_google_maps()
{
    // do something
}
function THEME_process_content_type_news()
{
    // do something
}
function THEME_process_content_type_event()
{
    // do something
}

If this is possible, what's the correct way of naming functions to do this?

1 Answer 1

2

In the case of the content types, Drupal invokes only the preprocess and process functions for the page; it doesn't invoke a different preprocess function basing on the content type of the node being shown. Drupal invokes the following functions (the same order is followed for the process functions):

  • template_preprocess(&$variables, $hook)
  • template_preprocess_page(&$variables)
  • MODULE_preprocess(&$variables, $hook)
  • MODULE_preprocess_page(&$variables)
  • ENGINE_engine_preprocess(&$variables, $hook)
  • ENGINE_engine_preprocess_page(&$variables)
  • ENGINE_preprocess(&$variables, $hook)
  • ENGINE_preprocess_page(&$variables)
  • THEME_preprocess(&$variables, $hook)
  • THEME_preprocess_page(&$variables)

"MODULE" is the short name of a module, "ENGINE" is the short name of the theme engine used for the currently used theme, and "THEME" is the short name for the currently set theme.

You could say to Drupal to call different preprocess functions, but the hook that would add them doesn't have information about the content type of the node being shown. hook_theme_registry_alter() could be used to add new preprocess functions to a theme function, but all the preprocess function added would be all executed.

function mymodule_theme_registry_alter(&$theme_registry) {
  if (isset($theme_registry['page']) {
    $theme_registry['page']['preprocess functions'][] = 'template_mymodule_additional_preprocess_handler';
    $theme_registry['page']['preprocess functions'][] = 'template_mymodule_last_preprocess_handler';
  } 
}

This is only necessary if the preprocess function is not one of the functions that Drupal would invoke.

If you need to use a different template file basing on the content type, then a preprocess function can suggest the template files to use; in Drupal 7, this means to use code similar to the following one:

function mymodule_preprocess_page(&variables) {
  if (!empty($variables['node']) {
    $variables['theme_hook_suggestions'][] = "page__" . str_replace(array("/", "\\", "\0", '-'), array('', '', '', '_'), $variables['node']->type);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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