Drupal 6 to 7, a first glimpse

Today I finally started porting a module over to Drupal 7. I didn't read up in depth on the changes and just dove in and started learning based on the PHP errors and features that were not working.

I'm not going to go into anything too in depth here as there is good information out there already, but I took some notes and will point out some changes I had to make with the module I was working on.

Admin settings path

Once updating the .info page so I could enable the module, I found the settings page listed at the top level of the administration area.

The admin settings path has changed form admin/settings/your-custom-module to admin/config/your-custom-module

Queries

The result of db queries now return objects that you can run foreach commands on. Here's an example:

<?php$rows = db_query("SELECT nid, title FROM {node} WHERE type = 'page'");foreach ($rows as $row) {  $output .= l($row->title, 'node/'. $row->nid);}

The way you add your variables in the mix has also changed. No need to wrap your tokens if it's a string anymore.

<?phpdb_query("UPDATE {table} SET field1 = :mystring, field2 = :myint", array(':mystring' => 'foo', ':myint' => 5);

drupal_get_form

drupal_get_form now only returns the form array and you need to call drupal_render on the form array if you want to get the HTML output.

print drupal_render(drupal_get_form('some_form'));

hook_nodeapi

I was working on another section of code that used hook_nodeapi, $op = 'view' to add content to the page view. hook_nodeapi is now gone and you will need to use hook_node_view. As they broke out each operation of nodeapi into its own hook, it will add more functions, but do away with the nodeapi monster logic we sometimes come across, or write ourselves.

drupal_to_js()

drupal_js is a function in D6 that outputs the data sent in to it as a JSON string. In D7, you should use json_js_output(). Alternatively if you would just like to get the string without printing it, use drupal_json_encode()