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.

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

// enable modules first

// now run their install files

// other magic functions that are only called when admin/build/modules form is submitted

// 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;