Upgrading modules to Drupal 7

Error message

The spam filter installed on this site is currently unavailable. Per site policy, we are unable to accept new submissions until that problem is resolved. Please try resubmitting the form in a couple of minutes.

With the recent release of Drupal 7, the community should now focus on upgrading existing Drupal 6 modules to Drupal 7.

Having already upgraded the print module from 4.7 to Drupal 5, and from that to Drupal 6, I can tell you that this time the process will be both harder and easier.

A lot harder

The Drupal 5.x to 6.x upgrade process had only 82 API changes. When upgrading from Drupal 6.x to 7.x, however, there are more than 200 changes.

One of the more visible changes is the introduction of the new database API, where this code in Drupal 6:

db_query("INSERT INTO {mytable} (intvar, stringvar, floatvar) VALUES (%d, '%s', %f)", 5, 'hello world', 3.14);
$id = db_last_insert_id();

becomes this in Drupal 7:

$id = db_insert('mytable')
  ->fields(array(
    'intvar' => 5,
    'stringvar' => 'hello world',
    'floatvar' => 3.14,
  ))
  ->execute();
?>

For further details, refer to the complete documentation on the new database API.

Another change, with some impact, is the removal of the $op parameter in important hooks such as hook_nodeapi which has now been replaced with hook_node_delete(), hook_node_insert(), hook_node_load(), etc.

I could go on to describe the remaining 200+ changes, but fortunately that information already exists in drupal.org

But a bit easier

The good news is that the people behind the coder module have developed two tools which simplify this process for you: coder review and coder upgrade.

Coder Review is the same module that already included rules on how to upgrade modules from 5.x to 6.x, 4.7.x to 5.x, coding standards, etc. There are already a couple of rules for detecting what you need to do to upgrade a module to Drupal 7. Unfortunately, a full list of rules has yet to be completed. However, running it through your module can be useful to detect the code that is incompatible with Drupal 7.

The really nice part is the new Coder Upgrade module. This sub-module is a lot more complete and will convert your code automatically by simply placing the existing Drupal 6 module in a specific directory, selecting it's files in the coder upgrade UI and pressing a button. Like magic, you'll receive a set of files that include the patch file applied to your module, and (hopefully) a Drupal 7 compatible version. Don't forget to follow the module's instructions and run Coder Review afterward to highlight the areas that need manual intervention. After that, it should be a simple case of installing the Drupal 7 version of your module and testing it.

If you're interested in trying Coder Upgrade, there's a site that allows you to run Coder Upgrade remotely.