Openlayers behavior

Jesper bisgaard

I recently worked on a site where they had a map of their stores which enabled users to search for their local store. Each store had a popup if you clicked on the icon with the store name and a link to the store page, where you could find opening hours and additional information about the perticular store.

The customer wanted to change this so clicking on the store icon would send the user directly to the store page instead of opening a popup. The map was implemented using openlayers and there wasn't an exsiting behavior for this, which gave me the oppotunity to try and make a new open layers behavior.

First of all you need to create a module or modify an existing and you would properly want to add a dependency on openlayers.

Next you need to add hook_openlayers_behaviors()

function my_module_openlayers_behaviors() {
  return array(
    'openlayers_behavior_my_behavior' => array(
      'title' => t('My Behavior'),
      'description' => t('On click go to store page'),
      'type' => 'layer',
      'path' => drupal_get_path('module', 'my_module') . '/includes/behaviors',
      'file' => '',
      'behavior' => array(
        'class' => 'openlayers_behavior_my_behavior',
        'parent' => 'openlayers_behavior',

This will notify openlayers that you have a new behavior in your includes/behaviors folder.

In the file you then declase a php class which describes how your behavior will work. In this class you have a number of options to acts on different stages of your behaviors life, but for this task we don't need to do much.

class openlayers_behavior_mybehavior extends openlayers_behavior {}
  function options_init() {
    return array();

  function options_form($defaults) {
    return array();

  function render(&$map) {
    drupal_add_js(drupal_get_path('module', 'mybehavior') . '/includes/behaviors/js/openlayers_behavior_my_behavior.js');
    return $this->options;

The options_init function provides initial values for the options_form function. The options_form allows you to provide a number of settings for your behavior. Many behaviors will need this function but it is not required.

The render function is where the magic happens and it is a required function for the behavior. Here you can return any custom markup for your behavior but our case only needs to provide a javascript file.

If you would like to see some examples of the behavior class, checkout the default behaviors provided with the openlayers module. Especially the popup behavior I found to be a good starting point.

Next we have our javascript.

Drupal.theme.prototype.openlayersLinkToNode = function(feature) {
  var output = '';
  return output;

// Make sure the namespace exists
Drupal.openlayers.linkToNode = Drupal.openlayers.linkToNode || {};

 * OpenLayers go to node Behavior
Drupal.openlayers.addBehavior('openlayers_behavior_link_to_node', function (data, options) {
  var map = data.openlayers;
  var layers = [];
  var selectedFeature;

  // For backwards compatiability, if layers is not
  // defined, then include all vector layers
  if (typeof options.layers == 'undefined' || options.layers.length == 0) {
    layers = map.getLayersByClass('OpenLayers.Layer.Vector');
  else {
    for (var i in options.layers) {
      var selectedLayer = map.getLayersBy('drupalID', options.layers[i]);
      if (typeof selectedLayer[0] != 'undefined') {

  // if only 1 layer exists, do not add as an array.  Kind of a
  // hack, see
  if (layers.length == 1) {
    layers = layers[0];

  var linkToNodeSelect = new OpenLayers.Control.SelectFeature(layers, {
      onSelect: function(feature) {
        var nid = feature.attributes.nid;
        window.location.href = 'node/' + nid;

  Drupal.openlayers.linkToNode.linkToNodeSelect = linkToNodeSelect;

Most of this javascript is there to ensure that the functionality works.

The important part of this script is the OpenLayers.Control.SelectFeature(layers, function) where we add our select functionality

There we go, I hope it helps you on your way to making the openlayers behaviors of your dreams. :)

Subscribe to Openlayers