Whenever the answer to a Drupal question, either here or on blogs and other articles, is to use a hook and then some example code is provided, what are the assumed steps from beginning to end for implementing this answer? These are likely left out to avoid redundancy but it's difficult to piece together the underlying, assumed foundation and skills.

Where does the given example code from many such answers to questions go? Is this essentially what building a module is? The concept of a hook is easy to understand and explained well here but how to use a specific hook to alter some piece of core to achieve something isn't clear.

For example, if the question is, "how do I change the text of such and such button" and the answer is "use hook hook_such_and_such_blahdy_blah" what does this actually mean?

Real world example: This blog about how to theme the comment block is great. But what is the author really saying to do when he says to just override the hook by copying the code from the API page and then alter the hook.

Resources I've checked out but don't mean anything without the missing foundation:


TL;DR version: Most hooks_something() go inside your custom module's .module, but not always. You can put alter hooks in your theme's template.php file for D7 or in .theme in D8.

There are several assumptions packed into those answers, and yeah they can be hard for people getting used to Drupal. Here are some assumptions cooked into those answers.

First, they assume you know have the tools and knowledge to write code for your project. Often people give those kinds of answers when asked a question where someone was looking for a module that already does what they are asking. Some of that is the nature of the community, and some of that is the nature of sites like this one.

Second, they assume you have a module or theme for custom code already started (or know how to start one). Lots of Drupal projects will have a module that is just there to provide a few needed hooks to handle an edge case.

Third, they assume you know when to use a module vs a theme for adding the hook. Some hooks really need to be in a module, some should be in a module, and some should be in a theme. But in the end that's is often use case specific (or in the case of agencies done to be consistent with seven sites already supported by the same team). Rarely do people give guidance which is the right choice.

Fourth, for some hooks they are assuming you know how to figure out how to get it called. Some Drupal 7 hooks (e.g. views) have a different pattern where you declare you have the hooks in your main .module file and then you define the hooks in their own file.

See also: Is it possible to define hook functions in template.php instead of the module?


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.