Enabling/Installing New Modules via Update.php: The Complete Solution

In our last episode of enabling new modules via update.php, Steve McKenzie pointed me to a better method: module_enable(). A quick test found, however, that it didn't run the install files, and didn't rebuild the module files cache. So after spending 5 minutes in system.module, I found all the missing pieces. The example update function below will install and enable the new module, as well as rebuild all the css, node type, and menu caches. In simple language, it does everything that happens when the modules admin page is submitted. Enjoy.

<?php
function example_update_1() {
$ret = array();

// your array of modules you wish to enable and install in the update
$modules = array('some_module', 'some_other_module');

// You must rebuild the module cache for the system table to see the modules
module_rebuild_cache();

// enable modules first
module_enable($modules);

// now run their install files
drupal_install_modules($modules);

// other magic functions that are only called when admin/build/modules form is submitted
menu_rebuild();
node_types_rebuild();
drupal_clear_css_cache();

// just a report for the install page -- otherwise this update will show up as "FAILED"
$ret[] = array('success' => true, 'query' => "enabled some module, and some other module");
return $ret;
}
?>