Use module_invoke() instead of calling the function directly
If you want to call a function from another module, use the module_invoke() hook instead of calling the function directly.
I ran into a situation where we had to throttled the statistics module. Errors started being thrown to the page because I was calling statistics_get() in my theme. Here's the code:
if ( module_exists('statistics') && user_access('view post access counter') ) {
$statistics = statistics_get($node->nid);
if ($statistics) {
$mylinks[] = format_plural($statistics['totalcount'], '1 read total', '@count reads total');
$mylinks[] = format_plural($statistics['daycount'], '1 read today', '@count reads today');
}
}
We were getting into trouble with the statistics_get() call in the second line. As you can see, even though we check for the existence of the module first with module_exists('statistics'), it didn't cover the case when the statistics module is enabled but throttled.
By using module_invoke() instead, this block won't throw errors even if the referred module is disabled or throttled.
To construct a module_invoke() call, simply make the 1st argument the name of the module, the 2nd the part of the function name after modulename_ (called the "hook" in Drupal speak), and the remaining arguments are what should be passed on to the function.
So this: statistics_get($node->nid)
Becomes this: module_invoke('statistics', 'get', $node->nid)
Back to my real world example. My above code that threw errors when statistics was throttled works perfectly fine after changing it to this:
if ( module_exists('statistics') && user_access('view post access counter') ) {
$statistics = module_invoke('statistics', 'get', $node->nid);
if ($statistics) {
$mylinks[] = format_plural($statistics['totalcount'], '1 read total', '@count reads total');
$mylinks[] = format_plural($statistics['daycount'], '1 read today', '@count reads today');
}
}
Full text blog feed