Couple of days back I was working with Openlayers in one of my project in D7. I got stuck with some functionality which forced me to make custom behavior which should work with Openlayer module in Drupal 7. I found Openlayers has very less documentation so I thought to write this blog. I will make some introduction about how to start with creating custom behavior which work with Openlayers. I am not focusing on some particular functionality but on giving base to your functionality.
I am assuming you know how to configure Openlayers and display map in Drupal 7. So , here’s the situation you faced that you need to achieve some functionality which Openlayers don’t provide and then you have to create some module which should work with Openlayers.
Next step is to make a custom module . Go to sites/all/modules/ and create your module directory containing files
- module_name.info
- module_name.module
Also create some more directory in your module directory like
- include — [It will contain all .inc files]
- js — [javascript files ]
- css — [css files ]
Now we will write some things in your modules .info file.
name = Module name
description = Allow you to draw polygon on map and it will search locations on map based on polygon draw.
core = 7.x
package = “Openlayers”
dependencies[] = openlayers
files[] = module_name.module
files[] = include/openlayers_behavior.inc
Step 2: We will add some code in modulename.module file.
<?php
// implementing hook_init()
function modulename_init(){
// here we are adding our css and javascript file
drupal_add_js(drupal_get_path(‘module’, ‘open_draw’) .
‘/js/openlayers_behavior_drawfeatures.js’);
drupal_add_css(drupal_get_path(‘module’, ‘open_draw’) .
‘/css/openlayers_behavior_drawfeatures.css’);
}
// implementing hook_openlayers_behaviors()
function modulename_openlayers_behaviors() {
return array(
‘openlayers_behavior_region_click’ => array(
‘title’ => t(‘Polygon search’),
‘description’ => t(‘It will allow search on maps when user will draw polygon on map’),
‘type’ => ‘layer’,
‘path’ => drupal_get_path(‘module’, ‘open_draw’) . ‘/include’,
‘behavior’ => array(
‘file’ => ‘openlayers_behavior.inc’,
‘class’ => ‘openlayers_behavior_drawfeatures’,
‘parent’ => ‘openlayers_behavior’,
),
),
);
}
Step 3: We will make openlayers_behavior.inc in our include directory and add some behavior in it.
<?php
/**
* behavior
*/
class openlayers_behavior extends openlayers_behavior {
/**
* Provide initial values for options.
*/
function options_init() {
return array(
‘element_id’ => ”,
‘feature_types’ => array(),
‘feature_limit’ => 0,
);
}
function options_form($defaults = array()) {
$features = array(
‘polygon’ => t(‘Polygon’),
);
return array(
‘feature_types’ => array(
‘#title’ => t(‘Available Features’),
‘#type’ => ‘checkboxes’,
‘#options’ => $features,
‘#description’ => t(‘Select what features are available to draw.’),
‘#default_value’ => isset($defaults[‘feature_types’]) ? $defaults[‘feature_types’] : array(),
),
);
}
}
That’s it now just enable your module and you can see your behavior in behavior section of Openlayers map settings.
Of course now our behavior are not doing anything we just able to see our behavior’s settings in Openlayers map settings. In order to make behavior functional now you have to just add javascript or Jquery in you /js directory and css in /css directory.