Mapping With Drupal - Alan Palazzolo [40]
*
* Provides basic examples to show how to extend the OpenLayers module.
*/
/**
* Implements hook_openlayers_behaviors().
*/
function mappingdrupal_ol_extensions_openlayers_behaviors() {
// For more documentation on writing OpenLayers behaviors,
// see the docs/BEHAVIORS.txt within the OpenLayers module.
return array(
'mappingdrupal_ol_extensions_geolocate' => array(
'title' => t('HTML Geolocate'),
'description' => t('Allows for geolocation of the user
utilizing HTML5.'),
'behavior' => array(
'path' => drupal_get_path('module', 'mappingdrupal_ol_extensions') .
'/behaviors',
'file' => 'mappingdrupal_ol_extensions_geolocate.inc',
'class' => 'mappingdrupal_ol_extensions_geolocate',
'parent' => 'openlayers_behavior',
),
),
);
}
The hook_openlayers_behaviors() function returns an array, which has the ID of the Behavior as its key. This array contains another array with the following data:
The title is the name of the Behavior and will show up in the administrative interface.
The description is used in the administrative interface as well, to give guidance on the purpose of the Behavior.
The behavior item is another array (Drupal loves nested arrays), describing where we will find the plug-in. This Behavior plug-in is a file.
The Behavior plug-in object
Now that we have told OpenLayers about where our Behavior plug-in file will be, we will need to create that file and define the Behavior behavior. We will add some configuration options to the Behavior so that the administrative user can choose how the map behaves. There are three options to be made available: whether to geolocate on page load; whether to create a button for geolocating; and what zoom level to go to when centering the map.
The first step is to create a subfolder in our module called behaviors. In that subfolder, we will create a new file named mappingdrupal_ol_extensions_geolocate.inc. The following is the complete file.
NOTE
Our Behavior does not necessarily have to be in a behaviors folder. We defined the name of the file and where it lives in hook_openlayers_behaviors() in Define Behavior.
In upcoming versions of the OpenLayers module, Behaviors will be using a new CTools interface for plug-ins; this will involve telling CTools where plug-ins are found, and then putting the .inc files in this place along with some extra metadata. However, the method described here will still work.
/**
* @file
* Implementation of OpenLayers behavior for HTML5 geolocation.
*/
/**
* Geolocation behavior.
*/
class mappingdrupal_ol_extensions_geolocate extends openlayers_behavior {
/**
* Provide initial values for options.
*/
function options_init() {
return array(
'on_load' => FALSE,
'button' => TRUE,
'zoom' => 11,
);
}
/**
* Provide form for configurations per map.
*/
function options_form($defaults) {
return array(
'on_load' => array(
'#title' => t('Geolocate on load'),
'#type' => 'checkbox',
'#description' => t('When checked, the map will geolocate the user
and zoom in when the map first loads.'),
'#default_value' => isset($defaults['on_load']) ?
$defaults['on_load'] : FALSE
),
'button' => array(
'#title' => t('Geolocate button'),
'#type' => 'checkbox',
'#description' => t('When checked, provides a button on the map that
will geolocate the user.'),
'#default_value' => isset($defaults['button']) ?
$defaults['button'] : TRUE
),
'zoom' => array(
'#title' => t('Zoom level'),
'#type' => 'textfield',
'#description' => t('Determine the zoom level when geolocating. Higher
is more zoomed in.'),
'#default_value' => isset($defaults['zoom']) ?
$defaults['zoom'] : 11
),
);
}
/**
* Render.
*/
function render(&$map) {
drupal_add_css(drupal_get_path('module', 'mappingdrupal_ol_extensions') .
'/behaviors/mappingdrupal_ol_extensions_geolocate.css');
drupal_add_js(drupal_get_path('module', 'mappingdrupal_ol_extensions') .
'/behaviors/mappingdrupal_ol_extensions_geolocate.js');
return $this->options;
}
}
That is a fair