1

I've been using Drupal now for a pretty long time, but this whole hooks and preprocessor thing still gets me confused. And I can't seem to really find a structured documentation to help.

Intuitively it feels like changing a contrib module is not part of theming, but more of module extending / modifying, but it is also theme-specific, so it's confusing to know if this topic is indeed related to the above or not.

Here are some of my questions that I am still left with:

1. are function hooks used in modules the same as theme hooks used in the theme?

2. if a contrib module uses a "hook" is this something i can override or extend in my theme?

*3. if a contrib module uses a hook, say: hook_block() can I then hook the hook to change the module's output?*

example: inside the contribmodule.module file:

contribmodule_block() { $variables['foobar_item'] = 'foobar item'; }

can this be overriden by the following in the template.php file?

contribmodule_preprocess_block() { $variables['foobar_item'] = 'new item'; }

Is there a guide for all the fundamental concepts? All i can find is the API for hooks, but that says really nothing at all about it other than giving a list of available hooks; zero theory there.

4. if a contrib module has a function which I can NOT override with a hook onto a hook, then how do i change what the contrib module does for my theme? am I forced to hack the code of the contrib module?

1 Answer 1

6
  1. Not quite. Generally speaking, normal hooks are used to control the behavior (functionality) while theme hooks are used to control the appearance of your site. If you want to understand more about hooks, you may want to read What are the different types of hooks?.
  2. Since Drupal 7, you can implement hooks in your theme. That said, the best practice is to put alterations to the site behavior in a custom module, and use your theme only for alterations to the appearance.
  3. That depends on the hook. The output of a block can be altered by implementing hook_block_view_alter.
  4. All modules that are built 'the Drupal way' should use theme hooks and render arrays to generate their output. You can always implement a preprocess function in your template.php to alter variables before they go to a template, or override a theme function or template to make it do what you want. See the Theme guide, more specifically Overriding themable output to learn more.

I hope that answers your questions.

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.