Top Drupal Hooks and Functions

Alternative title: What every new Drupal developer should know

Over the past few years I've been teaching various Drupal classes ranging from introductory courses to module development courses for seasoned developers. In fact what lit a fire under me to write up this entry was an upcoming course I'm doing with Chapter Three December 14-18th in San Francisco. I'm my classes nearly every student has asked me a variation of the same question; What do I need to know?

If the student was a beginner I'd point them to one of the numerous Top X Drupal modules, such as Lullabot's Top 80, or direct them to review the awesome site showcases up on drupal.org. If the student was a developer (or aspiring developer) the answer was never as concrete. I would explain the concept of a hook, rattle off a few hooks I thought were important, point them to greggle's (or GVS's?) awesome Cheat sheet, tell them about t(), hook_menu, explain how db_query() worked, and then direct them to http://api.drupal.org. Being the geek I am this wasn't good enough for me so I set out to find out what the top hooks and functions were. I wanted something concrete.

I broke out my terminal and used CVS to checkout every module on drupal.org that was listed as stable for Drupal 6 (those in a DRUPAL-6 branch), 3,308 modules. I then wrote a quick and dirty BASH script that went through every function sorting, counting, and ultimately discovering what the most heavily used hooks were. I wrote this script over a year ago at a time when I was living behind the great firewall of China, which made blogging, twittering, and other fun Internet activities a frustrating experience, which is why I have not posted this sooner.
Without much further delay here are the top hooks used in Drupal 6:

Hook name# of usesComments
hook_menu2,120
hook_uninstall1,659Very brief research showed that modules were implementing hook_uninstall to remove variables set in the administrative pages or with variable_set, which would explain why this has more implementations than hook_install
hook_install1,520
hook_perm1,374
hook_help1,232Wow! This is pretty awesome
hook_theme1,183
hook_schema999
hook_form_alter852
hook_nodeapi660
admin_settings591 This is not a hook but a common way to name your administrative settings function.

The next 12 significant implementations are:

Hook name# of usesComments
hook_block568
hook_init538
settings446Related to admin_setting in the top ten
hook_form437
hook_user354
hook_cron317
hook_enable273
settings_form265Related to admin_setting in the top ten
hook_load264
hook_views_api261
hook_delete241
hook_access237

Most Utilized Functions

My curiosity then led me to answer the question: What are the most frequented used functions in a Drupal module?
A few commands later I discovered 143,783 unique functions used a total of 1,008,398 times across the 3,308 modules. Below is how these panned out.
Note: I removed uses of if, foreach, etc.

Function name# of usesComments
array()200,886Proof positive Drupal developers love arrays. Note that this is technically a language construct and not a function.
t()107,388Hurray for translation!
db_query()24,266Drupal developers also love the database
variable_get()23,694
theme()8,283
drupal_set_message()8,260We love our messages
l()7,149
isset()6,331
db_fetch_object()6,072db_fetch_array came in at 1,931 uses
url()5,087
unset()4,654
define()4,350
variable_del()3,969
variable_set()3,854
check_plain()3,384
drupal_render()3,321
db_result()3,147
form_set_error()3,111
drupal_get_path()2,825
str_replace()2,633


For the curious here are the BASH commands I ran to discover this information

The completely inefficient but written in roughly ten minutes BASH script I used to discover the top hooks was: --count_functions.sh--

#!/bin/sh
module_dir="/Users/jredding/Downloads/contributions/modules"
for module_name in `find $module_dir -name *.info|xargs basename |awk -F.info '{print $1}'`
do
grep -hr "function * ${module_name}_" *|awk -F${module_name}_ '{print $2}'|awk -F"(" '{print $1}'
done

I then piped the output through

sort |uniq -c |sort -n 
Be aware that this method takes a very long time to process, roughly three to four hours. I later modified the script to the following, which only took about seven minutes to run:
functions_tmp=/tmp/all_functions.txt
module_dir="/Users/jredding/Downloads/contributions/modules"
grep -hR "^function *" modules/* > $functions_tmp

for module in `find $module_dir -name *.info|xargs basename |awk -F".info" '{print $1}'`
do
  grep -h "function * ${module}_" $functions_tmp |awk -F${module}_ '{print $2}'|awk -F"(" '{print $1}'
done
rm $functions_tmp
~                 
Of course I could probably save more time if I saved the data to two arrays and did all of this searching/sorting in memory instead of performing literally thousands of disk hits. I'll save that for another day.

To discover every function used I ran the following command:

find ./modules/ -exec awk '/[a-9]\(/ { for (i = 1; i <= NF; i++) printf "%s\n", $i }' {} \; |awk '/[a-9]\(/ {print}'|awk -F\( '{print $1}' |sort |uniq -c |sort


A few interesting stats

I found the following stats interesting.

#What
3,308Total modules analyzed
67,582Total number of defined functions
20-21Average number of functions per module
143,783Unique functions called including Drupal and PHP functions
1,008,398Total functions called including Drupal and PHP functions


Raw Data

Function use


I'm a Drupal lover & love

I'm a Drupal lover & love tweaking on it continuously to discover new tricks,but I really appreciate you for your tips here.Thanks.

Cheers

Found this really helpful, especially as I'm fairly new to Drupal. Will definitely be checking out hook_help

Always useful to have these

Always useful to have these big picture analysis of what people are doing. Thanks for taking the time to do this. I would expect form to feature much higher - and impressed by Views making the cut and, yes, good to see hook_help being used so much!

Awesome analysis

This is a really interesting breakdown. I've done developer training myself, and I've always been more likely to introduce hook_form_alter first. I may have to reassess now based on this :)

Thank you!

Thank you for taking the time to share and help, it's very useful!

Note: array() is a language

Note: array() is a language construct used to represent literal arrays, and not a regular function.

true.

true. I left out the others like "if" and jQuery's $ but left this one in because its usage was so high.

Interesting DB functions

If that drupal_result is indeed db_result then there are 3147+6072+1931 = 11150 fetch calls vs 24266 db_query which leads me to think that there are 13116 INSERT/UPDATE/DELETE queries vs 11150 SELECTs. drupal_write_record does not even appear on your list. Conclusion of all: people are writing DB queries all the time when they should not.

763

763 is the number of times drupal_write_record() appears, quite a bit less than db_query. I'm curious what will happen in Drupal 7 with the new db_select, db_merge, etc.

note: yes drupal_result should have been db_result.

Good job

Interesting article, I remember you telling us about this back in the training. I'm glad hook_help is being used that much too. :)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <img src> <img >
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options