Mapping With Drupal - Alan Palazzolo [35]
The assumption here is that you have your mappingdrupal_gmap_extensions.info file defined and now are working in a blank mappingdrupal_gmap_extensions.module file.
The first step is to define our new block with hook_block_info():
/**
* @file
* Main module file for Mapping with Drupal GMap Extensions module.
*
* Provides basic examples to show how to extend the GMap module.
*/
/**
* Implements hook_block_info().
*/
function mappingdrupal_gmap_extensions_block_info() {
// Provide a block for user to choose to geolocate
// themselves.
$blocks['gmap_geolocate'] = array(
'info' => t('GMap Geolocate'),
);
return $blocks;
}
The next step is to use the hook_block_view() function to create the link that will be displayed by the block:
/**
* Implements hook_block_view().
*/
function mappingdrupal_gmap_extensions_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'gmap_geolocate':
$block['subject'] = t('Geolocate Yourself');
$block['content'] = array(
'#theme' => 'mappingdrupal_gmap_extensions_block_content',
);
break;
}
return $block;
}
For the block output, we are using Drupal Render Arrays, a standard way of building a page in Drupal. We have to define a theme function for the rendering:
/**
* Implements hook_theme().
*/
function mappingdrupal_gmap_extensions_theme($existing, $type, $theme, $path) {
return array(
'mappingdrupal_gmap_extensions_block_content' => array(
'variables' => array(),
),
);
}
Finally, we define the default implementation of the theme function. Our goal here is to create a link in HTML that we can reference from our JavaScript; we will do this with a specific class name, “mappingdrupal-gmap-geolocate-action.” We will also hide this link using inline CSS in case the browser does not support HTML5 geolocation:
/**
* Theme callback for block content.
*/
function theme_mappingdrupal_gmap_extensions_block_content() {
return 'style="display:none;">';
}
Add geolocation options to maps
Our next step is to add some configuration options to the “Map Behavior Flags” form at admin/config/services/gmap. These configuration options will allow the administrator of the site to decide whether users should be geolocated automatically, or whether the link to geolocate will be displayed. This is accomplished by using the hook_gmap() function provided by the GMap module.
WARNING
At the time of this writing, this functionality did not work as intended. The options are available in the configuration form, but the chosen settings were not available to our code when rendering the map. Later in this exercise, where the bug impacts our code, we provide a way to work around this problem. This issue should be resolved soon. For updates you can read the bug report in the GMap module’s issue queue.
/**
* Implements hook_gmap().
*/
function mappingdrupal_gmap_extensions_gmap($op, &$map) {
// For documentation on this hook, look at the
// gmap.php file in the gmap module.
switch ($op) {
case 'behaviors':
return array(
'geolocate_auto' => array(
'title' => t('Geolocate user automatically'),
'default' => FALSE,
'help' => t('This will geolocate the user on each page load and
focus the map there.'),
),
'geolocate_block' => array(
'title' => t('Geolocate user with block link'),
'default' => FALSE,
'help' => t('This will geolocate the user only if they use
the action on the GMap Geolocate block.'),
),
);
}
}
The hook_gmap() function accepts an $op (operation) parameter to determine what part of rendering we are at, as well as a $map parameter so that we can alter the map along the way. The above code hooks into the behaviors operation to add two options: “Geolocate user automatically” and “Geolocate user with block link.” You will see these options in the GMap