Controlling execution order of Drupal modules

So lately, this had become a ‘stuff that breaks at work’ blog, but I fear that if I don’t doc­u­ment these, they’ll likely bite me again. And, if the solu­tions help some­one else, then that’s great.

Our team recently launched a large web­site in Dru­pal that uses many home-grown mod­ules. I dis­cov­ered that with cer­tain mod­ules, the exe­cu­tion order is very impor­tant; espe­cially authen­ti­ca­tion mod­ules. We’d devel­oped a mod­ule that inter­acts with the University’s sin­gle sign-on ser­vice and auto­mat­i­cally cre­ates or loads a Dru­pal user.

Order mat­ters

Every­thing worked per­fectly in test­ing except 403 han­dling. Specif­i­cally, when an unau­then­ti­cated user encoun­ters a page that requires authen­ti­ca­tion or a cer­tain role (via the pri­vacy by role mod­ule), the user would expect to be routed to the sin­gle sign-on form, authen­ti­cate, and then be returned to the resource they orig­i­nally requested. This wasn’t hap­pen­ing. The user would click a pro­tected link, login, and then encounter the default 403 page: ‘Access Denied.’ The give­away was that if you refreshed the page, the pro­tected page would appear. After hours of debug­ging, I dis­cov­ered that it was indeed the exe­cu­tion order. Our authen­ti­ca­tion mod­ule was being exe­cuted after the node load.

System.weight

Some Googling revealed the nearly undoc­u­mented ‘weight’ col­umn in the sys­tem table. This col­umn allows you to con­trol the order/priority in which mod­ules are exe­cuted. Most mod­ule will default to 0. To increase the pri­or­ity, decrease the weight; to decrease the pri­or­ity, increase the weight. With 90% of our mod­ules at 0, and, assum­ing that we’d always want authen­ti­ca­tion to run first (even before the Node mod­ule), we set its weight to –1. This caused the authen­ti­ca­tion to fire before the node load and suc­cess­fully load the requested page.

2 comments

  1. Hi,

    I just ran into the ‘Util­ity’ mod­ule (http://drupal.org/project/util) which allows you to mod­ify the weight of mod­ules via an admin interface.

  2. Hey there, I wanted to give my exam­ple from today. Your post gave me the idea that my prob­lem could be mod­ule exe­cu­tion order and it was! I was hav­ing to hide the image_attach form for a par­tic­u­lar role and ran into the prob­lem of mul­ti­ple hook_form_alter()‘s in dif­fer­ent mod­ules (in par­tic­u­lar image_attach.module). My new hook_form_alter() was in my ‘article.module’ and was being squashed by image_attach.module so I moved the form func­tion to my ‘item.module’ and it worked b/c it was fir­ing after image_attach. Thanks!
    –Jordan

Comment on this post

You may use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>