You are here

openlayers-api-hooks.html in Openlayers 6

File

help/openlayers-api-hooks.html
View source
This documentation can be found in the <em>docs/openlayers.api.php</em> file and may be more up to date and verbose.

<h3>OpenLayers Map Preprocess Alter</h3>
<p>Map array alter.  Fired before processing the array, and
before checking for errors.  The whole array is passed
along and will allow you to alter it in any way.  This
is a good place to alter the map, if the other hooks
do not provide the functionality you need.</p>

<h4>Parameters</h4>
<p>$map
Map array</p>

<h4>Code</h4>
<pre><code>
function hook_openlayers_map_preprocess_alter(&$map = array()) {
  // Do something to the $map
}
</code></pre>

<h3>OpenLayers Map Alter</h3>
<p>Map array alter.  Fired after preparing the array, and
before checking for errors.  The whole array is passed
along and will allow you to alter it in any way.  This 
is a good place to alter the map, if the other hooks
do not provide the functionality you need.</p>

<h4>Parameters</h4>
<p>$map
Map array</p>

<h4>Code</h4>
<pre><code>
function hook_openlayers_map_alter(&$map = array()) {
  // Do something to the $map
}
</code></pre>


<h3>OpenLayers Behaviors Info</h3>
<p>This hook tells OpenLayers about the available behaviors
that can be used by name in maps.</p>

<h4>Return value</h4>
<p>Return a nested associative array with the top level 
being a unique string identifier, and the nested array
containing the following key/pairs:
</p><ul><li>"name": Translated name of the behavior.
</li><li>"description": Translates description.
</li><li>"file": The Drupal path for where the PHP callback is stored
</li><li>"callback": The name of the PHP function that will be called
when the behavior is rendered
</li><li>"js_file": The Drupal path for where the JS callback is stored
</li><li>"js_callback": The name of the JS function that will be called
when the behavior is rendered.  This will be a function of the
OL.Behaviors object
</li></ul>

<h4>Code</h4>

<pre><code>
function hook_openlayers_behaviors_info() {
  // Taken from openlayers_behaviors.module

  $file = drupal_get_path('module', 'openlayers_behaviors') .'/includes/openlayers_behaviors.behaviors.inc';
  $js_file = drupal_get_path('module', 'openlayers_behaviors') .'/js/openlayers_behaviors.behaviors.js';
  $info = array();

  // Define info array
  $info['openlayers_behaviors_zoom_to_layer'] = array(
    'name' => t('Zoom to Layer'),
    'description' => t('When the map is finished loading, zoom to the features contained within the given layer'),
    'file' => $file,
    'callback' => 'openlayers_behaviors_process_zoom_to_layer',
    'js_file' => $js_file,
    'js_callback' => 'zoomToLayer',
  );

  return $info;
}
</code></pre>


<h3>OpenLayers Layer Handler Info</h3>
<p>Provides information on layer handlers.  Every layer needs
to have a valid type (layer handler).</p>

<h4>Parameters</h4>
<p>$map
Map array of map being rendered</p>

<h4>Return value</h4>
<p>Return a nested associative array with the top level 
being a unique string identifier key which corresponds to the
layers' types.  The next level being an array of key/value
pairs: 
</p><ul><li>"layer_handler": This is the JS callback name that will
belong to OL.Layers object.
</li><li>"js_file": The JS file to include to look for the callback.
</li></ul>

<h4>Code</h4>

<pre><code>
function hook_openlayers_layers_handler_info($map = array()) {
  // Take from openlayers.module

  return array(
    'WMS' => array(
      'layer_handler' => 'WMS',
      'js_file' => drupal_get_path('module', 'openlayers') .'/js/openlayers.layers.js',
    ),
    'Vector' => array(
      'layer_handler' => 'Vector',
      'js_file' => drupal_get_path('module', 'openlayers') .'/js/openlayers.layers.js',
    ),
  );
}
</code></pre>


<h3>OpenLayers Layers Info</h3>
<p>This hook tells OpenLayers about the available layers
that can be used by name in maps.  Layers can still be defined
manually, but this allows for easy calling of layers,
and these will show up in the Preset UI.</p>

<h4>Return value</h4>
<p>Return a nested associative array with the top level 
being a unique string identifier, and the nested array
containing the following key/pairs:
</p><ul><li>"name": Translated name of the layer.  This will show up
in the Preset UI.
</li><li>"description": Translates description.
</li><li>"file": The Drupal path for where the callback is stored
</li><li>"callback": The name of the PHP function that will be called
when the layer is rendered
</li><li>"projection": An array of projections that the layer is
compatible with.  Leave empty if compatible with all.
</li><li>"baselayer": Boolean whether the layer is a base layer
or not.
</li></ul>

<h4>Code</h4>

<pre><code>
function hook_openlayers_layers_info() {
  // Taken from openlayers.module

  // Define info array
  $info['openlayers_default_wms'] = array(
    'name' => t('Default OpenLayers WMS'),
    'description' => t('A simple basemap to get you started'),
    'file' => drupal_get_path('module', 'openlayers') .'/includes/openlayers.layers.inc',
    'callback' => 'openlayers_process_layers',
    'projection' => array('4326', '900913', '4269'),
    'baselayer' => TRUE,
  );

  return $info;
}
</code></pre>


<h3>OpenLayers Presets</h3>
<p>This hook lets other modules define map presets that
the user can choose from in various places, or
clone.</p>

<h4>Return value</h4>
<p>Return a nested associative array with the top level 
being a unique string identifier, and the nested array
containing the following key/pairs:
</p><ul><li>"preset_name": Unique string with only lowercase characters 
and underscores.
</li><li>"preset_title": Translated title to be used listing presets.
</li><li>"preset_description": Translated description.
</li><li>"preset_data": The unrenderd map array
</li></ul>

<h4>Code</h4>
<pre><code>
function hook_openlayers_presets() {
  // Taken from openlayers.module

  $presets = array();

  // Create map array
  $default_map = array(
    'projection' => '4326',
    'width' => 'auto',
    'default_layer' => 'openlayers_default_wms',
    'height' => '300px',
    'center' => array(
      'lat' => '0',
      'lon' => '0',
      'zoom' => '2',
    ),
    'options' => array(
      'displayProjection' => '4326',
    ),
    'controls' => array(
      'LayerSwitcher' => TRUE,
      'Navigation' => TRUE,
      'PanZoomBar' => TRUE,
      'MousePosition' => TRUE,
    ),
  );

  // Create full preset array
  $presets['default'] = array(
    'preset_name' => 'default',
    'preset_title' => t('Default Map'),
    'preset_description' => t('This is the default map preset that comes with the OpenLayers module.'),
    'preset_data' => $default_map,
  );

  return $presets;
}
</code></pre>


<h3>OpenLayers Style Info</h3>
<p>This hook tells OpenLayers about the available styles
that can be used by name in maps.  These will show up
in the Preset UI.</p>

<h4>Return value</h4>
<p>Return a nested associative array with the top level 
being a unique string identifier, and the nested array
containing the following key/pairs:
</p><ul><li>"name": Translated name of the style.
</li><li>"description": Translated description.
</li><li>"file": The Drupal path for where the PHP callback is stored
</li><li>"callback": The name of the PHP function that will be called
when the behavior is rendered
</li></ul>

<h4>Code</h4>
<pre><code>
function hook_openlayers_styles_info() {
  // Taken from openlayers.module

  // Define info array
  $info['default'] = array(
    'name' => t('Default Style'),
    'description' => t('Basic default style.'),
    'file' => drupal_get_path('module', 'openlayers') .'/includes/openlayers.styles.inc',
    'callback' => 'openlayers_process_styles',
  );
  $info['default_select'] = array(
    'name' => t('Default Select Style'),
    'description' => t('Default style for selected geometries'),
    'file' => drupal_get_path('module', 'openlayers') .'/includes/openlayers.styles.inc',
    'callback' => 'openlayers_process_styles',
  );

  return $info;
}
</code></pre>