Professional Drupal Web Developers in Atlanta, GA

Upgrading Modules and Themes from Drupal 6 to Drupal 7

Upgrading Modules and Themes from Drupal 6 to Drupal 7

We recently upgraded our site from Drupal 6 to Drupal 7. It was a satisfying process that taught us a bit about the differences between Drupal 6 and 7. During the course of the upgrade, we took notes on the differences we noticed in hopes of helping any other developers who are also making the upgrade to Drupal 7.

The following is a quick overview of a few key differences we noticed during the upgrade, and is not intended to be a comprehensive resource:

Themes

Regions are now rendered differently

Drupal 6
<?php print $region; ?>

Drupal 7
<?php print render($page['region']); ?>

Page template file has been split into two template files

In Drupal 7 there is now page.tpl.php and html.tpl.php instead of the Drupal 6 page.tpl.php. html.tpl.php now contains the HTML head and the body wrapper, while page.tpl.php just takes care of the page content, itself. To go along with the new tpl file, there is also a new template function, template_preprocess_html.

CSS

A number of css naming conventions have changed, notably blocks. For example, in Drupal 6 a block created in a module would be wrapped in the class, "block-module-block_name", while in Drupal 7 it is wrapped by "block-module-block-name". A small difference, but one that will break block-specific CSS in your Drupal 6 site. The structure and CSS in forms are slightly different, too. One difference is that a textarea is now wrapped in form-item like most other fields. So at least be aware that your CSS may initially break when upgrading.

Modules

Database queries

The overall approach to pulling values from a database query has changed. The functions "db_fetch_object", "db_fetch_array", and "db_result" are no longer part of the Drupal 7 API. Instead, the "db_query" function, itself, fetches the result as an object. With "db_result" gone, pulling a single result or count result from a database query has changed as well. Some other syntax has also changed, such as how to insert variables into your query.

Drupal 6  
<?php
// Multiple results
$query = db_query('SELECT name, tid FROM {term_data} WHERE
  vid = %d ORDER BY weight', array($vid));
while ($result = db_fetch_object($query)) {
  ...
}

// Single result or count
$result = db_result(db_query('SELECT tid FROM {term_data} 
  WHERE vid = %d AND name LIKE "%s"', array($vid, $name)));
?>

Drupal 7 
<?php
// Multiple results
$results = db_query('SELECT name, tid FROM 
  {taxonomy_term_data} WHERE vid = :vid ORDER BY weight', 
  array(':vid' => $vid));
foreach ($result as $results) {
  ...
}

// Single result or count
// where 0 indicates the first field that was 
// queried, in this case the only field
$result = db_query('SELECT tid FROM {taxonomy_term_data} 
  WHERE vid = :vid AND name LIKE :name', 
  array(':vid' => $vid, ':name' => $name))->fetchfield(0);
?> 

Database structure

As you may have noticed in the previous section, some of the tables in the database structure have different names. Taxonomy tables now have "taxonomy_" as a prefix. For example, "term_data" is now "taxonomy_term_data". Also to note with taxonomy tables is that "term_node" is now "taxonomy_index".

CCK fields, most of which are now part of Drupal core, have changed their table's naming structure in the database. In Drupal 6, CCK fields would have the prefix, "content_". Now in Drupal 7 they have the prefix, "field_data_". So as an example, the table "content_field_images" would now be named "field_data_field_images".

jQuery

In general, we discovered that in Drupal 7 you will need to write jQuery code using "jQuery" instead of the shortened "$". For any existing code there is a workaround. As an example:

Drupal 6
$('class').instruction;

Drupal 7
jQuery('class').instruction;
OR
(function($) {
  $('class').instruction;
  $(other code);
})(jQuery);

Drupal API functions

There have been a number of alterations to existing functions in Drupal 6 as well as a number of new functions that have been added to Drupal 7.

----- hook_block() -----

In our module upgrades, one of the most significant of these was related to the Drupal 6 function, hook_block. In Drupal 7, hook_block has been split into four new functions and a new alter function has been added

Drupal 6
function module_block ($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      ...
      break;
    case 'view':
      ...
      break;
    case 'save':
      ...
      break;
    case 'configure':
      ...
      break;
  }
} Drupal 7 function module_block_info () { ... } function module_block_view ($delta = '') { ... } function module_block_save ($delta = '', $edit = array()) { ... } function module_block_configure ($delta = '') { ... } function module_block_view_alter (&$data, $block) { ... } ----- drupal_get_form() -----

Another commonly used application that has changed is how to render forms. It is a simple change, but a significant one, nonetheless.

Drupal 6
<?php print drupal_get_form($form_id); ?>

Drupal 7
<?php print drupal_render(drupal_get_form($form_id)); ?>

----- l() -----

The link function, l(), has been altered in a slight way as well. Previously in Drupal 6 a class attribute was added by simply entering a string. If multiple classes were involved then you entered a space-separated string containing your different classes. Now in Drupal 7 the class attribute must be entered as an array. This is true whether there is a single class assigned or multiple classes assigned.

Drupal 6
<?php
print l('Link title', 'link-path', array(
  'attributes' => array(
    'class' => 'class1 class2',
  ),
),
);
?>

Drupal 7
<?php
print l('Link title', 'link-path', array(
  'attributes' => array(
    'class' => array(
      'class1', 
      'class2',
    ),
  ),
),
);
?>

File system

Something that affected a good bit of what we were doing inside custom modules and custom tpl files involved the $file object. Previously in Drupal 6, the $file object would contain a path to the file (image, document, etc.) relative to the base path of the site ($file->filepath). In Drupal 7 that has changed
($file->uri = public://image.jpg), and a new related function has been added, file_create_url() to allow us to use the new uri.

Drupal 6
<?php $filepath = base_path() . $file->filepath; ?>

Drupal 7
<?php $filepath = file_create_url($file->uri); ?>

Changes to the $file object can be explained to some degree by looking at the database structure. In Drupal 7, two new tables have been added that relate to files. These are "file_managed" and "file_usage". The "file_managed" table contains specific info related to the file, while the "file_usage" table points out a file's relationship to other entities like nodes..

A comprehesive list of changes and additions to note when upgrading Drupal modules or upgrading Drupal themes from version 6 to version 7 can be found on Drupal's site (themes - http://drupal.org/node/254940, modules - http://drupal.org/update/modules/6/7).

Are there any other specific changes from Drupal 6 to Drupal 7 that have thrown a wrench in your upgrade process?