initsoft
Distributing modules with install scripts
So you've created the next great Drupal module that change the world, it works, and you're all set to go. But then you consider what it takes to get the module running, and realize it's nontrivial for an ordinary user to get things up and running. Most of the contrib modules that you can pull from Drupal.org don't have this problem; normally one plugs them in, enables them, and away you go (with the occasional exeption of a module which needs external code added by hand due to license restrictions).
I've run into this problem a couple of times, and yesterday I figured out how to set things up for installation. Modules are allowed to have modulename.install scripts which export implementations of hook_install() and hook_uninstall(), which are invoked on a module being installed for the first time, and being uninstalled.
In my case, I want to enable a new CCK type upon my module being installed. This means that the module requires both the Content (CCK) module and Content Copy (which is distributed as part of CCK). To make a note of this, I added both modules to my dependency list in my modulename.info.
dependencies = content content_copy
Now that we have the dependencies being enforced by the system, let's fill out our install hook, shall we? In this case, we are going to import a CCK type that I had previously created upon module installation. I grabbed the code for doing this import from Snipplr.
Here's the contents of my modulename.install file:
/**
* Implementation of hook_install().
*/
function modulename_install() {
include_once('./' . drupal_get_path('module', 'node') . '/content_types.inc');
include_once('./' . drupal_get_path('module', 'content') . '/content_admin.inc');
$cck_definition_file = './' . drupal_get_path('module', 'modulename') . '/ccktype_export.txt';
watchdog('modulename', "loading CCK type from $cck_definition_file");
$values = array();
$values['type_name'] = '';
$values['macro'] = file_get_contents($cck_definition_file);
drupal_execute("content_copy_import_form", $values);
}
My uninstall hook is empty for the moment, but I would imagine I should remove or disable my custom CCK type on uninstall.
Also, note that disabling a module does not uninstall it (nor run the uninstall hook). To uninstall a module you disable it, then go to the 'Uninstall' tab on the page and from there select the module for deletion. It turns out that even then, the module is not completely cleaned out from the system. To completely remove the module (and, it turns out, to get the install hook to fire on module enable), you have to go into the database and remove the module reference from the system table. I haven't had the chance yet to see if removing the line from the system table is an action commonly taken by modules in their uninstall hooks.