Differentiating Between a Drupal Hook Implementation and a Regular Function

258 views Asked by At

I have a couple of questions on Drupal coding conventions that I haven't been able to gleam from the docs or the code.

Outside of knowing the name of every hook in Drupal, is there a way to differentiate a function that implements a hook from a function that's just providing a bit of functionality for a hook? Either something enforced via code or some convention?

Second, possibly related question. After reviewing the core modules, I've noticed that some functions are named with a leading underscore

function _node_rankings(SelectQueryExtender $query) {
    ...
}

What meaning is attached to the underscore? My assumption it's mimicking a "protected" convention, meaning that it should only be called from other functions in the node.module file; however, I couldn't find anything to confirm this.

I know about the Coding Standards, but they seem aimed at general PHP syntax, not conventions aimed at Drupal's internal systems.

2

There are 2 answers

2
Matt V. On BEST ANSWER

In answer to your first question, if you look at the code of most modules, the comments above hook implementations will typically say:

/**
 * Implementation of hook_foo().
 */

After working with Drupal a while, you'll start to recognize the most common hooks.

0
Vinicius Braz Pinto On

You're right, the underscore prefix in function names indicates that it should be treated as a private function, only being called by the module that declared it.

I don't know if this is in Drupal's official docs, but there are some posts on drupal.org confirming this (like this or this).

EDIT: and yes, it also works to avoid turning a "normal" function into a hook implementation (although you should try to not name functions after existing hooks).