Last updated June 10, 2011. Created by rmiddle on January 17, 2007.
Edited by danmuzyka, arianek, RoloDMonkey, NancyDru. Log in to edit this page.
In Drupal, 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 (including negative numbers) to get your module to execute before others. Or, you can set a high weight to execute after other modules.
Note: In Drupal 7, modules can vary the weights of their hooks to be more flexible. For details, see hook_module_implements_alter() in the API documentation.
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'");If you want to base your module's weight on another, you can use the following:
$weight = db_result(db_query("SELECT weight FROM {system} WHERE name = 'othermodule'"));
db_query("UPDATE {system} SET weight = %d WHERE name = 'mymodule'", $weight + 1);Note: if you are not creating your own module that needs to set its own weight you can also look at using the 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 December 18, 2010 showing the weights of many popular modules:
| Module | Weight |
|---|---|
| performance | 3000 |
| user_register_notify | 1002 |
| betterdate | 255 |
| notifications_content | 100 |
| devel | 88 |
| rules | 20 |
| rules_forms | 20 |
| top_buttons | 20 |
| nodewords_verification_tags | 16 |
| nodewords_extra | 14 |
| nodewords_basic | 12 |
| contemplate | 10 |
| ljxp | 10 |
| nodewords | 10 |
| token | 10 |
| views | 10 |
| fieldgroup | 9 |
| primary_term | 9 |
| tac_lite | 9 |
| taxonomy_access | 9 |
| nodeorder | 5 |
| og_vocab | 5 |
| project_package | 3 |
| og_forum | 2 |
| og_gradebook | 2 |
| print_pdf | 2 |
| project | 2 |
| project_release | 2 |
| calendar | 1 |
| calendar_ical | 1 |
| community_tags | 1 |
| date_repeat | 1 |
| location_addanother | 1 |
| location_fax | 1 |
| location_phone | 1 |
| og_access | 1 |
| print_mail | 1 |
| slideshow | 1 |
| taxonomy_defaults | 1 |
| workflow_named_transitions | 1 |
| authorship | 0 |
| autoload | 0 |
| comment_box | 0 |
| content | 0 |
| content_copy | 0 |
| content_permissions | 0 |
| content_profile | 0 |
| content_profile_registration | 0 |
| content_profile_tokens | 0 |
| date | 0 |
| date_api | 0 |
| date_locale | 0 |
| date_php4 | 0 |
| date_popup | 0 |
| date_timezone | 0 |
| date_tools | 0 |
| dblog_clear | 0 |
| dblog_common | 0 |
| dblog_ext | 0 |
| dblog_filters | 0 |
| dblog_host_filter | 0 |
| dblog_time_filters | 0 |
| dblog_user_filter | 0 |
| devel_generate | 0 |
| devel_node_access | 0 |
| enforce_revlog | 0 |
| erp | 0 |
| erp_access | 0 |
| erp_accounting | 0 |
| erp_asterisk | 0 |
| erp_audit | 0 |
| erp_cart | 0 |
| erp_cashflow_projection | 0 |
| erp_cash_sale | 0 |
| erp_contact | 0 |
| erp_customer | 0 |
| erp_email | 0 |
| erp_formfixes | 0 |
| erp_franchise | 0 |
| erp_goods_receive | 0 |
| erp_google_maps | 0 |
| erp_invoice | 0 |
| erp_item | 0 |
| erp_job | 0 |
| erp_job_billable | 0 |
| erp_og_integration | 0 |
| erp_payment | 0 |
| erp_price_list | 0 |
| erp_price_list_update | 0 |
| erp_pricing | 0 |
| erp_purchase_order | 0 |
| erp_quote | 0 |
| erp_recurring | 0 |
| erp_referrer | 0 |
| erp_reports | 0 |
| erp_sms | 0 |
| erp_sms_job_notify | 0 |
| erp_sms_message | 0 |
| erp_store | 0 |
| erp_supplier | 0 |
| erp_tax | 0 |
| erp_timesheet | 0 |
| erp_warranty | 0 |
| filefield | 0 |
| filefield_meta | 0 |
| gmap | 0 |
| gmap_location | 0 |
| gmap_macro_builder | 0 |
| gmap_taxonomy | 0 |
| gradebook | 0 |
| gradebookapi | 0 |
| image | 0 |
| imageapi | 0 |
| imageapi_gd | 0 |
| imageapi_imagemagick | 0 |
| imagecache | 0 |
| imagecache_ui | 0 |
| imagefield | 0 |
| image_attach | 0 |
| image_gallery | 0 |
| image_import | 0 |
| image_im_advanced | 0 |
| jcalendar | 0 |
| jstools | 0 |
| location | 0 |
| location_cck | 0 |
| location_generate | 0 |
| location_node | 0 |
| location_search | 0 |
| location_taxonomy | 0 |
| location_user | 0 |
| logged_in | 0 |
| log_clear | 0 |
| messaging | 0 |
| messaging_debug | 0 |
| messaging_mail | 0 |
| messaging_mime_mail | 0 |
| messaging_phpmailer | 0 |
| messaging_privatemsg | 0 |
| messaging_simple | 0 |
| messaging_twitter | 0 |
| messaging_xmpp | 0 |
| mimemail | 0 |
| mimemail_compress | 0 |
| module_weights | 0 |
| nodereference | 0 |
| nodereference_url | 0 |
| notifications | 0 |
| notifications_autosubscribe | 0 |
| notifications_lite | 0 |
| notifications_tags | 0 |
| notifications_ui | 0 |
| notifications_views | 0 |
| notify | 0 |
| number | 0 |
| oauth_common | 0 |
| oauth_common_consumerui | 0 |
| oauth_common_providerui | 0 |
| og | 0 |
| og_actions | 0 |
| og_notifications | 0 |
| og_views | 0 |
| optionwidgets | 0 |
| perms_fieldsets | 0 |
| pm_block_user | 0 |
| pm_email_notify | 0 |
| 0 | |
| privatemsg | 0 |
| privatemsg_filter | 0 |
| project_legacy_paths | 0 |
| project_solr | 0 |
| project_usage | 0 |
| rules_admin | 0 |
| rules_scheduler | 0 |
| rules_test | 0 |
| securesite | 0 |
| simpletest | 0 |
| system_module | 0 |
| tac_lite_create | 0 |
| tagadelic | 0 |
| taxonomy_theme | 0 |
| text | 0 |
| themekey | 0 |
| themekey_debug | 0 |
| themekey_ui | 0 |
| tokenSTARTER | 0 |
| token_actions | 0 |
| 0 | |
| type_defaults | 0 |
| userreference | 0 |
| util | 0 |
| views_attach | 0 |
| views_export | 0 |
| views_gallery | 0 |
| views_gallery_og | 0 |
| views_ui | 0 |
| workflow | 0 |
| workflow_access | 0 |
| xmppframework | 0 |
| xmpp_api | 0 |
| xmpp_client | 0 |
| xmpp_node_muc | 0 |
| xmpp_notifications | 0 |
| xmpp_relationships | 0 |
| xmpp_roster | 0 |
| xmpp_user | 0 |
| xmpp_vcard | 0 |
| xmpp_xmlrpc | 0 |
| getid3 | -10 |
| boost | -90 |
| trace | -99 |
Comments
Strict update query
If you want to be really strict, and make sure that only the module weight gets updated you should also use the type='module' part in your query. This might be useful when you have a theme and a module with the same name (you are advised to avoid this to begin with, but as a module contributor you never know what themes or theme engines might be enabled on someone else's Drupal site).
db_query("UPDATE {system} SET weight = [yournumber] WHERE type = 'module' AND name = 'yourmodulename'");If you want to base your module's weight on another, you can use the following:
$weight = db_result(db_query("SELECT weight FROM {system} WHERE type = 'module' AND name = 'othermodule'"));db_query("UPDATE {system} SET weight = %d WHERE type = 'module' AND name = 'mymodule'", $weight + 1);
D7 version of the query
For D7 with the new abstraction layer will be:
<?php$weight = db_select('system', 's')
->fields('s', array('weight'))
->condition('name', 'othermodule', '=')
->execute()
->fetchField();
db_update('system')
->fields(array('weight' => $weight +1))
->condition('name', 'yourmodulename', '=')
->execute();
?>