node links order and module weight

31 Aug 2010

I had my modules updated and voila! a new problem waiting to be solved.

The node-links section is being shared by the "print" (now "Printer, e-mail and PDF versions" module) and "service_links" modules. After the update process, the service links had been placed between "print" and "email" (parts of the print module). I don't like this.

So… how can someone manipulate the order of the objects in the links section? If you simply want the objects of one module to be in front (or after) the other module's objects, the answer is "module weight".

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'");

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 Module Weight (5.x) or Utility (6.x) 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.



I checked the print module .install file and noticed that it sets a weight value of 0. The print-mail (email) .install sets 1. Service_links module's .install file sets nothing - defaults to 0. That's the reason why service links had been placed between print and email.

Therefore, I added in the service_links.install file, the code below:

* @file
* Installation file for service links module.
function service_links_install() {
 // Module weight
 update_sql("UPDATE {system} SET weight = 2 WHERE name = 'service_links'");

Now service links module is heavier than print module and its links will be placed after print and email links.

The last step is to upload the new .install file, uninstall completely service_links and reinstall.