I was asked to do a presentation on coding in Drupal for our local Drupal user group. “Coding” is a pretty general topic, and the audience at our user group is pretty varied. So, I was not sure what specifically I should present that would encompass “coding”. As the time came along and I had to make a decision, I looked to the module that I currently spend most of my free time developing: OpenLayers.
What is OpenLayers?
Where Drupal Fits In
Drupal is a great content management system and development framework. OpenLayers fits well into this because it basically is a really great visualization tool for content (the most obvious visualization being maps). Drupal can basically provide the ability to create and management data/content and OpenLayers can be a fun vehicle to displaying that content.
The Drupal OpenLayers Module has actually been around for almost 2 years. Started by bdragon, he saw early on its strength of offering a nice interface for creating features. His work was later merged with crischan. All that was pretty much scrapped and the module was restarted once again in late March of 2009.
Having worked with Brandon before, I approached him to see what he was planning with the module and if I could help out at all. We discussed plans to create a simple API and a CCK widget for the Geo field.
Pretty much the day after Brandon and I talked about what we wanted to do with the module, phayes released the geo_gui. I wrote a post about how it would be nice to have Drupal’s GIS efforts a little more in sync, and Patrick was quick to respond.
About this Discussion
This article is mostly about how we have developed this module, and what kind of practices, standards, techniques we used along the way. Please keep in mind, at the time of this writing, we are still actively developing, and with all programming, there is always another way to do the same thing.
Some assumptions I want to make up front as we start getting into the code:
- You have built a Drupal site from start to finish, and understand the basic concepts of Drupal.
- You have a good understanding of important contributed modules like Views and CCK.
- You are familiar with mapping on the web, meaning you have used Google Maps before.
- You know PHP, meaning you know the syntax and can read it on a simple level.
- You know HTML and CSS. You should be able to write both of these as these are the building blocks of any modern website.
You can learn a lot of these technologies and more at the W3 Schools site.
Drupal Coding References
The following are good references for writing Drupal code (assuming Drupal 6):
- Coding Guidelines
- Module Building
- Writing Good Code
- Contributing to Drupal
- An associative array to hold all the data that is needed to render a map.
Please note that the following code is from a version that is still in development, and at a point that we have not taken the time to fully audit our code and release the module.
A Basic Map Array and Sensible Defaults
A very basic map array is below. Note the different depths as these usually mean separate processing.
This function also points out an important topic: Sensible Defaults. Sensible defaults basically mean that given no user input or customization, how will the system react. This is important from a usability standpoint, as it means that the module works without any configuration. Specifically for this module, it means that a map can be rendered without having to set any values.
Rendering the Map
The following if the rendering function for a map. The input is the associative array talked about above.
The rendering process actually involves 3 different hooks (though some are within sub-functions):
This hook gathers information on layers that are available to a map. The main goal of this hook is to get data on how to get more detailed data. This is important so that we are not unnecessarily processing a lot of data until the right time.
This is an alter hook, which is a little more specific that a regular hook. It’s most prominent use is with Drupal forms. This basically passes around the map array to any module that wants it to see if there is any final processing to do.
The OpenLayers Layers module, at the moment, is just implementation of the first two hooks. In the future it will become an actual interface for managing layers, but will still remain implementations of these hooks.
The following is the very top level of the map array processing. We are sending the map array with drupal_add_js($array, ‘setting), which can be accessed with something like:
Open Discussion and Collaboration
After too many emails, we decided to take out discussion out in the open about how to develop this module. This allows for us to have a more focused conversation and for other people to join in on the conversation.
One of the best parts of this project is collaborating with other developers around the world. One of the major strengths of open-source is having code reviewed by many people, and this project has been a small example of that. We are able to make the project much better by working together and discussing openly how and why we do things. Though we may not always agree on the best way to do things, we are able to discuss openly and come to a compromise and learn from each other.