Nick Lewis: The Blog | Drupal, CSS, Web Development, and the Myriad Iconoclasts

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;
}
?>

Enabling New Modules Via Update.php

UPDATE: There’s a better way.

I work with 3 other developers, all of whom have their own local sandbox of our site. Since we’re constantly adding new modules, I found a simple way to enable a new module via another module’s .install file. That way, all we have to do is run update.php when we update our source tree.

Here’s a simple example update function:

<?php

function some_enabled_module_update_1() {
 
$ret = array();
  switch (
$GLOBALS[‘db_type’]) {
    case
‘mysqli’:
    case
‘mysql’:
     
//this function tells drupal to update its file directories, and insert records for any new modules it sees.
     
module_rebuild_cache();
     
$ret[] = update_sql(« UPDATE {system} SET status = 1 WHERE name = ‘some_completely_new_module' »);
      break;
  }
  return
$ret;
}
;
?>

The major limitation of this method is that it doesn’t run the new modules install file. I can live with that. It beats always bugging other developers to see if the build needs a new module enabled. I’m sure there’s a workaround for that anyhow.

Nick Lewis: The Blog | Drupal, CSS, Web Development, and the Myriad Iconoclasts

Blogged with the Flock Browser

Tags: ,

Laisser un commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion / Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion / Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion / Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion / Changer )

Connexion à %s

%d blogueurs aiment cette page :