You are here

gm3_filter.module in Google Maps API V3 7

File

gm3_filter/gm3_filter.module
View source
<?php

/**
 * Enables a user to embed a google map within filtered text simply by adding
 * 
 * [google_map]
 * 
 * {gm3_json_specification}
 * 
 * [/google_map]
 */

/**
 * Implementation of hook_menu().
 */
function gm3_filter_menu() {
  return array(
    'gm3_filter' => array(
      'title' => 'Google Map filter helper',
      'description' => 'Generate the code required to embed a Google Map within a webpage.',
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        'gm3_filter_helper_form',
      ),
      'access callback' => 'gm3_filter_helper_form_access',
      'file' => 'gm3_filter.form.inc',
    ),
  );
}

/**
 * Implementation of hook_library().
 */
function gm3_filter_library() {
  return array(
    'filter_helper' => array(
      'title' => t('Filter helper code'),
      'website' => '',
      'version' => 1,
      'js' => array(
        array(
          'data' => drupal_get_path('module', 'gm3_filter') . "/js/gm3_filter.js",
        ),
      ),
      'dependencies' => array(
        array(
          'gm3',
          'gm3',
        ),
        array(
          'gm3',
          'gm3.point',
        ),
        array(
          'gm3',
          'gm3.polygon',
        ),
        array(
          'gm3',
          'gm3.polyline',
        ),
        array(
          'gm3',
          'gm3.rectangle',
        ),
        array(
          'gm3_region',
          'region',
        ),
        array(
          'gm3_field',
          'field_point',
        ),
        array(
          'gm3_field',
          'field_polygon',
        ),
        array(
          'gm3_field',
          'field_polyline',
        ),
        array(
          'gm3_field',
          'field_rectangle',
        ),
        array(
          'gm3_region',
          'region',
        ),
      ),
    ),
  );
}

/**
 * Access callback for the gm3_filter form.
 */
function gm3_filter_helper_form_access() {
  global $user;
  return TRUE;
}

/**
 * Implementation of hook_filter_info().
 */
function gm3_filter_filter_info() {
  return array(
    'gm3_filter_google_map' => array(
      'title' => 'Google Map',
      'description' => 'Embed a Google Map into any formatted text on your site',
      'process callback' => 'gm3_filter_google_map_process',
      'cache' => FALSE,
      'tips callback' => 'gm3_filter_google_map_tips',
      'weight' => 0,
    ),
  );
}

/**
 * Callbacks as defined in hook_filter_info.
 */
function gm3_filter_google_map_process($text, $filter, $format, $langcode, $cache, $cache_id) {

  // Search for [google_map]...[/google_map]
  preg_match_all('|\\[google_map\\]([^\\[]*)\\[/google_map\\]|i', $text, $matches);
  foreach ($matches[0] as $key => $match_text) {

    // Decode middle!
    if ($map = json_decode(trim($matches[1][$key]), TRUE)) {
      $text = str_replace($match_text, theme('gm3_map', array(
        'map' => $map,
      )), $text);
    }
    else {

      // Failed to decode the JSON, give an error message instead.
      $text = str_replace($match_text, '<p class="error">' . t('The google map you have provided is invalid.') . '</p>', $text);
    }
  }
  return $text;
}

Functions

Namesort descending Description
gm3_filter_filter_info Implementation of hook_filter_info().
gm3_filter_google_map_process Callbacks as defined in hook_filter_info.
gm3_filter_helper_form_access Access callback for the gm3_filter form.
gm3_filter_library Implementation of hook_library().
gm3_filter_menu Implementation of hook_menu().