6

It's more general question. Just to clarify. I can use preprocess function in my template.php file in a way:

function mytheme_preprocess_node(&$vars) {
  // Do some stuff
}

But is there a way to preprocess node in a module? I've been searching for function hook_preprocess_node() in drupal API, but there's no such function for D7, although it does exists for all other versions of drupal, including 8. May I treat template_preprocess_node() as a function which I can use in my module? For example, if my module has name 'custom' then can I use it this way?

function custom_preprocess_node(&$vars) {
  // Do some stuff
}

How can I override node in custom module?

6

You sure can. hook_preprocess_HOOK() is what you're looking for. From the docs...

Preprocess theme variables for a specific theme hook.

This hook allows modules to preprocess theme variables for a specific theme hook. It should only be used if a module needs to override or add to the theme preprocessing for a theme hook it didn't define.

6

In Drupal7, both modules and themes are able to use preprocess functions in the same way, and a given theme hook can have many preprocess implementations, originating from both modules and themes.

As with anything like this in the Drupal framework, knowing the order in which these functions run is important. Preprocess implementations from modules run first, and implementations by themes run last.

A quick search of Drupal core modules will give you examples in the node and rdf modules.

0

Have found good article about this http://themery.com/dgd7/advanced-theming/preprocess-process/implement

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.