Posted By

berkes on 01/03/08


system drupal install hooks weight

Versions (?)

Who likes this?

2 people have marked this snippet as a favorite


Assure hooks in a Drupal module are ran before the others.

 / Published in: PHP

Sets a module's weight to the lowest number. This assures that your module is ran before all other modules. Userfull if you want your implementation of a hook to be ran first. Add this to your .install file. Replace your_modulename with the name of your module.

  1. function your_modulename_install() {
  2. switch ($GLOBALS['db_type']) {
  3. case 'mysql':
  4. case 'mysqli':
  5. $weight = db_result(db_query('SELECT MIN(weight) AS weight FROM {system} WHERE status = 1')) - 1;
  6. db_query("UPDATE {system} SET weight = %d WHERE name = '%s'", $weight, 'your_modulename');
  7. break;
  8. default:
  9. drupal_set_message(t('Your database type is not supported'));
  10. }
  11. }

Report this snippet  


RSS Icon Subscribe to comments
Posted By: oadaeh on January 3, 2008

Nice. How about making sure it's the last one run (warning: untested): $weight = dbresult(dbquery('SELECT MAX(weight) AS weight FROM SYSTEM WHERE status = 1')) +1;

Posted By: WimLeers on January 8, 2008

Your db_query syntax is not quite right. "SYSTEM" instead of "{system}". "weight = %s" instead of "weight = %d".

Posted By: berkes on January 15, 2008

@wimLeers: correct. I've corrected this.

Posted By: berkes on January 15, 2008

@oadaeh, that is about it. If I need a module to run last, and therefore developed that code, i'll submit it on this site :)

You need to login to post a comment.