How to update a module's weight

Last modified: December 5, 2008 - 23:26

In Drupal 4.7 and later the order in which a module's hooks get called is dependent on the weight of your module in the system table. You can set a low weight (negative number) to get your module to execute before others. Or, you can set a high weight to execute after other modules.

Code to update weight

You will want to modify and then place this code into your module's modulename.install file in a modulename_install function. See more details on the hook_install in the hook_install API documentation.

db_query("UPDATE {system} SET weight = [yournumber] WHERE name = 'yourmodulename'");

Note: if you are not creating your own module that needs to set its own weight you can also look at using the Module Weight or Utility module for an admin interface to change weights of modules installed on your site.

Module weights in use

Core modules all use a weight of zero. Here is a table created on January 17, 2007 showing the weights in use in the contributed modules:





FileWeight set
modules/authorship/authorship.install1001
modules/autologout/autologout.install1000
modules/betterdate/betterdate.install255
modules/boost/boost.install-90
modules/category/contrib/cac_lite/cac_lite.install9
modules/category/contrib/cac_lite/cac_lite.install9
modules/community_tags/community_tags.installtaxonomy + 1
modules/contemplate/contemplate.install10
modules/devel/devel.install88
modules/erp/erp_formfixes/erp_formfixes.install1
modules/erp/erp_job_billable/erp_job_billable.install1
modules/feed/feed.install1
modules/forms_no_js/forms_no_js.module1
modules/httpauth/httpauth.module-50
modules/jstools/jscalendar/jscalendar.install10
modules/ljxp/ljxp.install10
modules/na_arbitrator/na_arbitrator.install9
modules/nodegoto/nodegoto.install10
modules/nodeorder/nodeorder.install5
modules/nodewords/nodewords.install10
modules/notify/notify.installscheduler + 10
modules/og2list/og2list.installog+1 or 1
modules/og_forum/og_forum.install2
modules/og_galleries/og_galleries.install5
modules/og_gradebook/og_gradebook.install2
modules/og_vocab/og_vocab.install5
modules/primary_term/primary_term.install9
modules/project/project.install2
modules/project/project.install2
modules/slideshow/slideshow.install1
modules/tac_lite/tac_lite.install9
modules/taxonomy_access/taxonomy_access.install9
modules/taxonomy_defaults/taxonomy_defaults.install-1
modules/taxonomy_theme/taxonomy_theme.install-10
modules/trace/trace.install-99
modules/views/views.install10
modules/workflow_named_transitions/workflow_named_transitions.installworkflow + 1
modules/user_register_notify..install1002

And if you want to base your weight on other module

alexis - September 7, 2007 - 21:30

You could run something like this in your module's .install file:

$weight = (int) db_result(db_query("SELECT weight FROM {system} WHERE name = 'othermodule'"));
db_query("UPDATE {system} SET weight = %d WHERE name = 'mymodule'", $weight + 1);

Although a correctly setup .info file shouldn't allow you to enable a module if another one is required this code can be helpful in a few situations.

Alexis Bellido
Ventanazul: web development and Internet business consulting shouldn't be boring

good idea

greggles - September 8, 2007 - 20:52

Before incorporating this into the document can you clarify the use of the (int) ?

What is the benefit of that?

Thanks!

--
Knaddisons Denver Life | mmm Chipotle Log

...

dmitrig01 - September 15, 2007 - 14:51

you can't add 1 to a string
[Drupal++]

check schema

greggles - September 16, 2007 - 13:03

How do you expect to get a string back from the system table's weight column?

mysql> desc system;
+----------------+--------------+------+-----+---------+-------+
| Field          | Type         | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+-------+
| filename       | varchar(255) | NO   | PRI |         |       |
| name           | varchar(255) | NO   |     |         |       |
| type           | varchar(255) | NO   |     |         |       |
| description    | varchar(255) | NO   |     |         |       |
| status         | int(11)      | NO   |     | 0       |       |
| throttle       | tinyint(4)   | NO   |     | 0       |       |
| bootstrap      | int(11)      | NO   |     | 0       |       |
| schema_version | smallint(6)  | NO   |     | -1      |       |
| weight         | int(11)      | NO   | MUL | 0       |       |
+----------------+--------------+------+-----+---------+-------+
9 rows in set (0.00 sec)

--
Knaddisons Denver Life | mmm Chipotle Log

Since there's no penalty for

jough - October 31, 2007 - 19:30

Since there's no penalty for explicit type casting in PHP, you may as well get in the habit of making sure you're doing math on a number, as PHP will evaluate a non-empty string to "1" which may not be what you were expecting.

 
 

Drupal is a registered trademark of Dries Buytaert.