sacha chua :: living an awesome life

2142 blog subscribers
2747 on Twitter
Subscribe!
E-mail Feed reader

Drupal: Programmatically installing and enabling modules in the .install file

To make configuration management easier, we decided to make sure that all behavior-related changes are in the source code repository. So when I needed to add the reCAPTCHA module to the project, I needed to figure out how to programmatically install and enable the module with update code in another module’s .install file.

Here is some sample code to do so:

/**
 * Install and enable the captcha module.
 */
function yourmodule_update_1() {
  $ret = array();
  include_once('includes/install.inc');
  module_rebuild_cache();
  drupal_install_modules(array('recaptcha'));
  variable_set('recaptcha_public_key', 'PUBLIC KEY GOES HERE');
  variable_set('recaptcha_private_key', 'SECRET KEY GOES HERE');
  $ret[] = array(
    'success' => true,
    'query' => 'Installed recaptcha module and enabled it',
  );
  return $ret;
}
So-soHmmGoodGreatAwesome! (No Ratings Yet)
Loading ... Loading ...
Save to - del.icio.us - Digg it - reddit - StumbleUpon -

3 Responses to “Drupal: Programmatically installing and enabling modules in the .install file”

  1. Just wondering how much this technique scales.

    We are been trying various techniques to automate the config replication with out manual work.

    So I would like to know how you handle when the configs get large and diverse. I think it will become a tedious job very fast.

  2. We tend to not have very diverse changes, because there’s really only one site (multisite with Domain Access) for this codebase, and now there are two branches: trunk and devel. I’ll post more notes as we try working with the two branches.

    Doing configuration changes in code is certainly easier to deploy across multiple systems than trying to do everything through the web-based system, for sure. =) Putting things in the .install file means that it’s easy to remember what to do after an update, because I’ll always run update.php. You can automate as much as you can, then display messages about other updates required.

    With the naming system of Drupal’s .install update functions, though, you could get conflicts if more than one developer adds update functions to the same file without checking the other person’s changes. I guess people could update their copies and check before committing, but it might get tricky when you’re merging changes across branches. I’m sure Drupal developers have thought about this before. If we run into that problem or we think we’ll run into that problem, I’ll post my notes here.

  3. Hi, this is my first time visiting your site. I am new to this website development and I am not in this profession, this is only my hobby. I am using Drupal with my site. I encounter many difficulties; like terminologies, codes, block visibility, user access, and even simple permission to a file. I made it though through persistence. I am glad I found your site via Google. I just want to keep in touch to developers in our country. I think your post is great, but I don’t have any idea on how to implement it.

Discussion Area - Leave a Comment

Please comment as you, not your organization.





On This Day...

  • 2009: Brainstorming around Smart Work — IBM’s holding another one of its awesome collaboration jams (72-hour web-based brainstorming/discussion), this time on Smart Work. I’m passionate about helping [...]
  • 2008: Drupal: Making our build system better — Hooray! We have running code, and we’re about to make another release after our code exits quality assurance. This means, [...]
  • 2006: Domain name — At some point in time, I think I should go for figuring out how to do my mail properly so that [...]
  • 2006: Emacs clinic at the Linux Caffe — Quinn Fung needed some help with Muse and RDF so that she could easily generate RSS feeds from Emacs, so [...]
  • 2006: Emacs: Changing the font size on the fly — I have a tiny laptop: 8.9″ diagonally. With a 1024×768 pixels screen resolution, things can get *pretty* small. The following functions [...]
  • 2006: Getting sound to work again — Things to remember when setting up sound in Ubuntu Linux on a Sony Vaio U1: modprobe trident modprobe snd_trident Be very very thorough [...]
  • 2005: Discovering exercise — Today I not only discovered my inner nerd, but I also started down the long and slippery path of cultivating [...]
  • 2003: Hyper in Emacs — Apparently, hyper in Emacs is C-x @ h if you haven’t bound a key to it. To wit, C-h c C-x [...]
  • 2003: SSH for DOS — Apparently, there _is_ SSH for DOS: http://sourceforge.net/projects/sshdos/
  • 2003: Debian pre-installed laptops — Doh! Debian.org already has a page that lists vendors that will pre-install Debian here: http://www.debian.org/distrib/pre-installed
  • 2003: canna-chasendic — a Japanese dictionary for ChaSen derived from Canna dictionary — * Package name : canna-chasendic Version [...]
  • 2003: Filipino speech corpus — From Ramil Sagum: the link for http://www.upd.edu.ph/~dsp/speech.html is wrong. =) whoever updated the site preffered .htm’s rather than .html’s. The Filipino speech corpus [...]