Drupal - Are there any naming conventions for helper functions in modules?
How to name a function is described in Naming Conventions which says:
Functions should be named using lowercase, and words should be separated with an underscore. Functions should in addition have the grouping/module name as a prefix, to avoid name collisions between modules.
That is valid for hooks and other functions implemented from the module.
For functions that are not hooks, you could also prefix them with an underscore, right before the grouping/module name. If you do it, you are saying to the developers of other modules that the function is not supposed to be called from other modules, but it is private for the module defining it.
This is a practice followed from PHP which is also followed from Drupal. For example, theme()
had been renamed _theme()
, before being removed, to make clear it was a private functions modules should not call. It's described in theme()
renamed to _theme()
and should not be called directly.
The direct callability of
theme()
was removed in favor of building render arrays consistently. It has been renamed to_theme()
and is for internal use only. Build render arrays instead of using_theme()
in your code for the following reasons.Calling the
_theme()
function directly raises several issues:
- It circumvents caching.
- It circumvents defaults of types defined in
hook_element_info()
, including attached assets- It circumvents the
pre_render
andpost_render
stages.- It circumvents Javascript states information.