You are here

function theme_gmap in GMap Module 7

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

Gmap element theme hook

1 theme call to theme_gmap()
gmap_element_info in ./gmap.module
Implement hook_element_info().

File

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

Code

function theme_gmap($variables) {
  $element = $variables['element'];

  // Track the mapids we've used already.
  static $mapids = array();
  _gmap_doheader();
  $mapid = FALSE;
  if (isset($element['#map']) && $element['#map']) {

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

    // Settings overrides it.
    $mapid = $element['#gmap_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 (!isset($element['#gmap_settings'])) {
    $element['#gmap_settings'] = array();
  }

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

  // Styles is a subarray.
  if (isset($element['#gmap_settings']['styles'])) {
    $map['styles'] = array_merge($mapdefaults['styles'], $element['#gmap_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'] = array_merge($element['#attributes']['class'], array(
    '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']) . '><noscript>' . t('Javascript is required to view this map.') . '</noscript></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;
}