avocado shake (dot) net

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');
        }
      }

Avocado Shake (dot net) is the personal website of Gregory Go, co-founder of Killer Aces Media and Drupal fanboy.

Full text blog feed

My Tumblog

FriendFeed Stream

Essential Skills of a Community Manager
From Google Reader, posted Saturday, July 26, 2008 - 21:12.
Cartoon: Fannie Mae & Freddie Mac
From Mixx, posted Saturday, July 26, 2008 - 21:04.
Essential Skills of a Community Manager
From Google Reader, posted Saturday, July 26, 2008 - 20:24.
Picturing Casualties In Iraq: Slide Show
From Mixx, posted Saturday, July 26, 2008 - 20:20.
Submitted: 19 Tips for Cheering Yourself Up -- From 200 Years Ago
From Mixx, posted Saturday, July 26, 2008 - 20:03.
Submitted: 3 Tips for Losing Weight on a Budget
From Mixx, posted Saturday, July 26, 2008 - 19:50.
Submitted: How to Deal with a Partner That Hides Money Problems
From Mixx, posted Saturday, July 26, 2008 - 18:10.
Blogging's Glass Ceiling (Kara Jesella/New York Times)
From Google Reader, posted Saturday, July 26, 2008 - 18:02.
How to increase submit speed from 45s to a fraction while keeping Akismet anti-spam - Pligg Forum
From del.icio.us, posted Saturday, July 26, 2008 - 17:26.
Lazy Linux: 10 essential tricks for admins
From del.icio.us, posted Saturday, July 26, 2008 - 16:21.