Install Drupal Modules Programatically

In order to make releases repeatable, to be able to test updates and share work will colleagues it is really useful to install new modules (and disable old ones) in update hooks.

This way one just has to update the code, then run update.php and you have the latest version of the site.

It also makes it easy to pull in a copy of the live database and check that the update still works (because it's easy you actually find yourself doing it).

Well that's the logic, but it wasn't entirely obvious to me how to do this.

The function module_enable() looks handy - but this doesn't run install hooks.

drupal_install_modules() is actually what you need - but you also need to clear the cache after installing.

<?php
function mymodule_update_6103(){
   
$items = array();
   
drupal_install_modules(array( 'path_redirect', 'tagadelic'));
   
module_disable(array('oldmodule'));
   
drupal_flush_all_caches();
    return
$items;
}
?>

It would be nicer still if I returned something useful in the $items array ...

Tags

Comments

Repeatability

I think it is a great thing to do. Taking a hint from Rails migrations. Anyone wanting to do serious testing or continuous integration needs to do stuff like this. I really recommend looking at the install_profile_api module to allow you to do all sorts of things. For instance within a update function

Creating taxonomy:

  install_include(array('taxonomy'));
  $props = array('module' => 'taxonomy', 'multiple' => 1);
  install_taxonomy_add_vocabulary('Plants', array('article' => 'article'), $props);

  $props = array('module' => 'taxonomy');
  install_taxonomy_add_vocabulary('Asia', array('article' => 'article'), $props);
  install_taxonomy_add_vocabulary('Europe', array('article' => 'article'), $props);

Adding content types

  install_include(array('node'));
  $props = array(
    'description' => 'Plant articles',
  );
  install_create_content_type('plant', 'Plant articles', $props);
  $props = array(
    'description' => 'Article',
  );
  install_create_content_type('article', 'Article', $props);

Permissions and roles

  install_include(array('user'));
  $moderator_rid = install_add_role('moderator');
  $contributor_rid = install_add_role('contributor');
  $developer_rid = install_add_role('developer');
  install_add_permissions($contributor_rid, array('post comments', 'post comments without approval'));
  install_add_permissions($moderator_rid, array('administer comments', 'administer users'));

Comments

Install profile API

Hi Stew,
cheers for the extra examples

There's a great presentation here on Drupal deployment which mentions the install profile api

http://www.slideshare.net/eaton/drupal-deployment-presentation

Personally I think this stuff should be in core - and I don't see why it shouldn't be added to over time in core!

But then I'm a self confessed Drupal heretic :-)

Post new comment

Got something to add - just enter a comment
all other fields are optional.

Your email address will not be published.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image without spaces, also respect upper and lower case.