You are here

function theme_gmap in GMap Module 6

Same name and namespace in other branches
  1. 5 gmap.module \theme_gmap()
  2. 6.2 gmap.module \theme_gmap()
  3. 7.2 gmap.module \theme_gmap()
  4. 7 gmap.module \theme_gmap()

Gmap element theme hook

8 theme calls to theme_gmap()
gmap_location_author_block_view in ./gmap_location.module
gmap_location_block_view in ./gmap_location.module
gmap_location_node_page in ./gmap_location.module
Draws a page with a google map with the node on it, or if no node is set all of the nodes on it.
gmap_location_user_page in ./gmap_location.module
Draws a page with a google map that has all of the site users.
gmap_set_location in ./gmap.module
Location chooser utility function.

... See full list

File

./gmap.module, line 877
GMap -- Routines to use the Google Maps API in Drupal.

Code

function theme_gmap($element) {

  // Usability: Prevent js errors on first visit to settings page, etc.
  // Of course it will still error if the *wrong* key is on file.
  if (gmap_get_key() == '') {
    return t('Unable to render map: Google Maps API key is missing.');
  }

  // Track the mapids we've used already.
  static $mapids = array();
  _gmap_doheader();

  // Convert from raw map array if needed.
  if (!isset($element['#settings'])) {
    $element = array(
      '#settings' => $element,
    );
  }
  $mapid = FALSE;
  if (isset($element['#map']) && $element['#map']) {

    // The default mapid is #map.
    $mapid = $element['#map'];
  }
  if (isset($element['#settings']['id'])) {

    // Settings overrides it.
    $mapid = $element['#settings']['id'];
  }
  if (!$mapid) {

    // Hmm, no mapid. Generate one.
    $mapid = gmap_get_auto_mapid();
  }

  // Push the mapid back into #map.
  $element['#map'] = $mapid;
  gmap_widget_setup($element, 'gmap', $mapid);
  if (!$element['#settings']) {
    $element['#settings'] = array();
  }

  // Push the mapid back into #settings.
  $element['#settings']['id'] = $mapid;
  $mapdefaults = gmap_defaults();
  $map = array_merge($mapdefaults, $element['#settings']);

  // Styles is a subarray.
  if (isset($element['#settings']['styles'])) {
    $map['styles'] = array_merge($mapdefaults['styles'], $element['#settings']['styles']);
  }
  gmap_map_cleanup($map);

  // Add a class around map bubble contents.
  // @@@ Bdragon sez: Becw, this doesn't belong here. Theming needs to get fixed instead..
  if (isset($map['markers'])) {
    foreach ($map['markers'] as $i => $marker) {
      if (isset($marker['text'])) {
        $map['markers'][$i]['text'] = '<div class="gmap-popup">' . $marker['text'] . '</div>';
      }
    }
  }
  switch (strtolower($map['align'])) {
    case 'left':
      $element['#attributes']['class'] .= ' gmap-left';
      break;
    case 'right':
      $element['#attributes']['class'] .= ' gmap-right';
      break;
    case 'center':
    case 'centre':
      $element['#attributes']['class'] .= ' gmap-center';
  }
  $style = array();
  $style[] = 'width: ' . $map['width'];
  $style[] = 'height: ' . $map['height'];
  $element['#attributes']['class'] = trim($element['#attributes']['class'] . ' gmap gmap-map gmap-' . $mapid . '-gmap');

  // Some markup parsers (IE) don't handle empty inners well. Use the space to let users know javascript is required.
  // @@@ Bevan sez: Google static maps could be useful here.
  // @@@ Bdragon sez: Yeah, would be nice, but hard to guarantee functionality. Not everyone uses the static markerloader.
  $o = '<div style="' . implode('; ', $style) . ';" id="' . $element['#id'] . '"' . drupal_attributes($element['#attributes']) . '>' . t('Javascript is required to view this map.') . '</div>';
  gmap_module_invoke('pre_theme_map', $map);
  if (isset($mapids[$element['#map']])) {
    drupal_set_message(t('Duplicate map detected! GMap does not support multiplexing maps onto one MapID! GMap MapID: %mapid', array(
      '%mapid' => $element['#map'],
    )), 'error');

    // Return the div anyway. All but one map for a given id will be a graymap,
    // because obj.map gets stomped when trying to multiplex maps!
    return $o;
  }
  $mapids[$element['#map']] = TRUE;

  // Put map data in a setting.
  drupal_add_js(array(
    'gmap' => array(
      $element['#map'] => $map,
    ),
  ), 'setting');
  return $o;
}