Backdrop Intro: Converting a Module, Part 1

XML Sitemap Module

In my last post in this series, I started going down the theming rabbit hole for Backdrop. A lot of that was very similar to my experience with Drupal 7 (D7), but separating the regions from the theme files was a really nice addition. 

Now, we are going to dive into more of a back-end-related discussion. To do so, we'll be converting the XML Sitemap module to be able to run on Backdrop. It was the most popular module I could see, other than CTools, that hadn't been ported to Backdrop. Since it has no dependencies, I thought it would be good for me to start on this one. 

The .info File

A couple changes have been made to the .info file for a module just like there had been for Backdrop themes. The two biggest are the addition of a "type" key and the autoloading of files. 

I decided to start on the 7.x-2.2 tag of the XML Sitemap module since the dev branches can have a lot of interesting bits of code in them...After posting a polite message in the issue queue, I first changed the .info file to look like this:

name = XML sitemap
description = Creates an XML sitemap conforming to the protocol.
package = XML sitemap
type = module
backdrop = 1.x
core = 7.x
; files[] = xmlsitemap.module
; files[] =
; files[] =
; files[] =
; files[] =
; files[] =
; files[] =
; files[] = xmlsitemap.install
; files[] = xmlsitemap.test
; recommends[] = robotstxt
configure = admin/config/search/xmlsitemap

It doesn't appear that the "recommends" key works when I had it uncommented...but I clearly need to clean up the required files part. Let's see if the module listing page has the XML Sitemap module listed.

Modules Listing Page

Success! We have our module recognized by Backdrop core. Now, we need to move onto enabling the modules. 

Enabling Modules

Once you no longer see a red x on the modules listing page, you can go ahead and try to enable the module. Crossing your fingers is not required, but it sure does seem to help me out :)

I would do a search for any "variable_" functions as those can be used in install hooks and are not compatible with Backdrop's new Configuration Management (CM) system. The XML Sitemap module didn't have any calls to those variable functions so I went ahead and took the plunge. 

Initially, when I tried to enable the XML Sitemap and XML Sitemap Custom modules, I got an error about not having a function that was needed. I'm not sure yet how Backdrop differs from Drupal 7 in handling module dependencies, but I suspect that was the issue. When I enabled them separately, the error vanished and both modules enabled themselves. Yay!

Autoloading Files

My next step was to actually generate a sitemap, since that's what the sitemap module is supposed to do. On first pass, I immediately ran into the issue of the class registry being replaced by a static class map (change record). Backdrop couldn't find the class being called in one part of a module file.  

What this means is that any file that was included in the .info file needs to be loaded another way. When Drupal got to a function that was called in a .module or .inc file that it didn't see in the file, the other files listed in .info were searched for that function...or Drupal might have loaded all of the files at once. 

However, now you need to provide classes in a hook for Backdrop to call when requested. I haven't looked into the internals here, but I'm guessing the change was made for lazy loading and saving resources. So, I added the following to my .module file:

 * Implements hook_autoload_info().
function xmlsitemap_autoload_info() {
  return array(
    'XMLSitemapWriter' => '',

This got me past one error during sitemap generation but straight into another one. Backdrop was complaining that there was no "language" column in the "url_alias" table. Good point Backdrop, there is no column like that anymore. 

From language to langcode

So, apparently the in the alias table and maybe elsewhere, the "language" column was changed to "langcode". I saw two queries to the database in an XML Sitemap generation function that I changed, and I tried again. To my surprise, I had a working sitemap!

XML Sitemap Module

Next Steps

So, of course there are plenty of other issues I'll need to work on for the XML Sitemap module, but I'm happy to say I only spent maybe two hours doing the initial port of this module and writing this article. You can find the code here to try and test yourself:

I encourage you to try and port a Drupal 7 module to Backdrop. I know you'll learn a lot in the process, and you'll also get a little more familiar with a new, emerging CMS in the process!