Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Convert modules from 6.x to 7.x

Convert modules from 6.x to 7.x

http://dublin2010.drupalcampireland.org/sessions/convert-modules-6x-7x

Now that Drupal 7 is upon us, I would like to explain the resources available to convert existing 6.x modules to 7.x.
I will summarize the major changes in the API, and where to get further information. I will also present the coder upgrade module which eases this process. Finally I will present a brief example of how to apply this process in an existing module.

João Ventura

December 09, 2010

Transcript

16 slides
  1. About me  'print' module mantainer  Using Drupal since

    2004  1 patch in Drupal 7 core :)  100% drupal developer for >1yr now  Now working at Trellon
  2. Thanks  Drupal Ireland for organizing this  First Drupalcamp!

     Trellon for sponsoring my trip  Stella for inviting me to propose a session  Guiness  For my headache.. :)
  3. API changes  82 API changes to take into account

    when converting a module from Drupal 5 to Drupal 6  19 + 8 + 8 + 20 + 10 + 7 + 22 + 25 + 41 + 24 + 15 + 15 + 6 + 13 + 5 + 1 – 5 = 234 API changes
  4. Database API - Select // Drupal 6 $result = db_query("SELECT

    nid, title FROM {node} WHERE uid = %d AND type = '%s'", 5, 'page'); // Drupal 7 $result = db_query("SELECT nid, title FROM {node} WHERE uid = :uid AND type = :type", array( ':uid' => 5, ':type' => 'page', ));
  5. Database API – Results // Drupal 6 while ($record =

    db_fetch_object($result)) { // Do stuff with $record, which is an object } // Drupal 7 foreach ($result as $record) { // Do stuff with $record, which is an object }
  6. Database API - Insert // Drupal 6 db_query("INSERT INTO {mytable}

    (intvar, stringvar, floatvar) VALUES (%d, '%s', %f)", 5, 'hello world', 3.14); $id = db_last_insert_id(); // Drupal 7 $id = db_insert('mytable') ->fields(array( 'intvar' => 5, 'stringvar' => 'hello world', 'floatvar' => 3.14, )) ->execute();
  7. Database API - Update // Drupal 6 db_query("UPDATE {node} SET

    title='%s', status=%d WHERE uid=%d", 'hello world', 1, 5); // Drupal 7 db_update('node') ->fields(array('title' => 'hello world', 'status' => 1)) ->condition('uid', 5) ->execute();
  8. Database API - Delete // Drupal 6 db_query("DELETE FROM {node}

    WHERE uid=%d AND created < %d", 5, REQUEST_TIME - 3600); // Drupal 7 db_delete('node') ->condition('uid', 5) ->condition('created', REQUEST_TIME - 3600, '<') ->execute();
  9. hook_nodeapi → hook_node_*  hook_nodeapi() is now:  hook_node_delete(), 

    hook_node_insert(),  hook_node_load(),  hook_node_prepare(),  hook_node_prepare_translation(),  hook_node_search_result(),  hook_node_presave(),  hook_node_update(),  hook_node_update_index(),  hook_node_validate(), and  hook_node_view().
  10. hook_nodeapi → hook_node_* // Drupal 6 function book_nodeapi(&$node, $op, $teaser,

    $page) { switch ($op) { case 'load': // Load a book. } } // Drupal 7 function book_node_load($nodes, $types) { // Load a book. }
  11. The good news  Some nice Drupal people have written

    the coder_upgrade module that simplifies the upgrade task for you.  It's a sub-module of the coder module.  Requires the grammar_parser module.  It can either do a review of the code and provide a list of required changes or;  It can automatically apply those changes
  12. Coder review / upgrade Coder includes two useful functionalities: 

    Review  Allows you to check your code against the Drupal coding standards  Some rules regarding stuff that should be done to convert modules from 6.x to 7.x  Upgrade  Upgrades your code from 6.x to 7.x, providing an upgraded version and the patch.
  13. Reference  Converting 6.x modules to 7.x (http://drupal.org/update/modules/6/7)  Coder

    module (http://drupal.org/project/coder)  Coder upgrade  #D7CX  http://cyrve.com/d7cx  http://drupal.org/project/modules?text=d7cx
  14. Questions  Any questions?  Shameless plug:  You're invited

    to our Drupalcamp in Lisbon in March 2011 (TBC).

More Decks by João Ventura

Other Decks in Programming