You are here

leaflet_geojson.module in Leaflet GeoJSON 7.2

Same filename and directory in other branches
  1. 8 leaflet_geojson.module
  2. 7 leaflet_geojson.module

API Extension for using Leaflet with GeoJSON that currently just allows to add a bbox strategy.

File

leaflet_geojson.module
View source
<?php

/**
 * @file
 * API Extension for using Leaflet with GeoJSON that currently just allows to add a bbox strategy.
 */

/**
 * Add a Bounding Box Strategy
 *
 * @param $source_info
 *   The source info as specified in hook_leaflet_geojson_source_info().
 */
function leaflet_geojson_add_bbox_strategy($source_info) {

  // Add bounding box javascript.
  drupal_add_js(drupal_get_path('module', 'leaflet_geojson') . '/leaflet.bbox.js', array(
    'weight' => 5,
  ));

  // Add custom settings.
  // The JS now expects an array even if there's only one layer, so be sure
  // the single layer is nested properly (the keyname is not important).
  // @TODO fix this.
  if (isset($settings['view_display'])) {

    // Create the base settings to be compatible with the new Views GeoJSON.
    // This will happen when the map block is built by leaflet_geojson_bean.
    $settings = $source_info;

    // Then add a copy in leaflet_1 for bean.
    $settings['leaflet_1'] = $source_info;
  }
  else {

    // This will happen when the block is placed by the ctools (panels) plugin.
    $settings = $source_info;
  }
  drupal_add_js(array(
    'leafletBBox' => $settings,
  ), 'setting');
}
function leaflet_geojson_source_get_info($source = NULL, $skip_cache = FALSE) {
  if (!$skip_cache) {
    static $drupal_static_fast;
    if (!isset($drupal_static_fast)) {
      $drupal_static_fast['leaflet_geojson_source_info'] =& drupal_static(__FUNCTION__);
    }
    $source_info =& $drupal_static_fast['leaflet_geojson_source_info'];
    if (empty($source_info)) {
      if ($cache = cache_get("leaflet_geojson_source_info")) {
        $source_info = $cache->data;
      }
    }
  }
  if (empty($source_info)) {
    $source_info = module_invoke_all('leaflet_geojson_source_info');

    // Let other modules alter the source info.
    drupal_alter('leaflet_geojson_source_info', $source_info);
    cache_set("leaflet_geojson_source_info", $source_info);
  }
  if (empty($source)) {
    return $source_info;
  }
  elseif (isset($source_info[$source])) {
    return $source_info[$source];
  }
}

/**
 * Implements hook_leaflet_geojson_source_info().
 */
function leaflet_geojson_leaflet_geojson_source_info() {
  $sources = array();
  $views = views_get_all_views();
  foreach ($views as $view) {
    foreach ($view->display as $display_name => $display) {
      $view
        ->set_display($display_name);
      $style_plugin = $view->display_handler
        ->get_option('style_plugin');
      $url = $view->display_handler
        ->get_url();

      // Make GeoJSON sources from the views_geojson module.
      if (in_array($style_plugin, array(
        'views_geojson',
        'views_geojson_feed',
      )) && !empty($url)) {

        // Build the display title for the admin UI.
        $display_title = '(' . $display_name . ')';
        if (!empty($display->display_title)) {
          $display_title = $display->display_title . ' ' . $display_title;
        }
        $title = $view->human_name . ' - ' . $display_title;

        // Set the layer title for map display.
        if (isset($display->display_options['title'])) {
          $layer_title = $display->display_options['title'];
        }
        if (!isset($layer_title) || $layer_title == '') {

          // Fallback to display_name since it is always set.
          $layer_title = $display_name;
        }
        $source = array(
          'id' => $view->name . '_' . $display_name,
          'title' => $title,
          'layer_title' => $layer_title,
          'type' => 'views_geojson',
          'url' => url($view->display_handler
            ->get_option('path'), array(
            'absolute' => TRUE,
          )),
        );

        // Determine if we should use a BBox strategy.
        if ($arguments = $display->handler
          ->get_option('arguments')) {
          foreach ($arguments as $id => $argument) {
            if (strpos($id, 'bbox') !== FALSE && $argument['default_argument_type'] == 'querystring') {
              $source['bbox'] = TRUE;
              if (isset($argument['default_argument_options'])) {
                $source['bbox_arg_id'] = $argument['default_argument_options']['arg_id'];
              }
            }
          }
        }

        // Custom views_geojson attributes.
        $source['view'] = $view->name;
        $source['view_display'] = $display;
        $sources[$source['id']] = $source;
      }
    }
    $view
      ->destroy();
  }
  return $sources;
}

/**
 * Implements hook_ctools_plugin_directory().
 */
function leaflet_geojson_ctools_plugin_directory($owner, $plugin_type) {
  if ($owner == 'ctools' && $plugin_type == 'content_types') {
    return 'plugins/' . $plugin_type;
  }
}