You are here

function geolocation_googlemaps_field_formatter_view in Geolocation Field 7

Implements hook_field_formatter_view().

File

modules/geolocation_googlemaps/geolocation_googlemaps.module, line 192
Google Maps widget and formatters for Geolocation.

Code

function geolocation_googlemaps_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $settings = $display['settings'];
  $settings['api_key'] = variable_get('geolocation_googlemaps_api_key', '');
  $element = array();
  $lat_key = 'lat';
  if (strpos($settings['map_height'], 'px')) {
    $height = strtok($settings['map_height'], 'p');
  }
  elseif (strpos($settings['map_height'], '%')) {
    $height = strtok($settings['map_height'], '%');
  }
  elseif (!strpos($settings['map_height'], '%') || !strpos($settings['map_height'], 'px')) {
    $height = $settings['map_height'];
  }
  if (strpos($settings['map_width'], 'px')) {
    $width = strtok($settings['map_width'], 'p');
  }
  elseif (strpos($settings['map_width'], '%')) {
    $width = strtok($settings['map_width'], '%');
  }
  elseif (!strpos($settings['map_width'], '%') || !strpos($settings['map_width'], 'px')) {
    $width = $settings['map_width'];
  }

  // To make this widget compatible with geofiled we need to rename the keys for
  // longitude. Geofield uses "lon" while Geolocation Field uses "lng".
  $lng_key = $field['type'] == 'geofield' ? 'lon' : 'lng';
  switch ($display['type']) {
    case 'geolocation_googlemaps_static':
      foreach ($items as $delta => $item) {
        $query = array(
          'zoom' => $settings['map_zoomlevel'],
          'size' => $width . "x" . $height,
          'format' => $settings['map_imageformat'],
          'maptype' => $settings['map_maptype'],
          'markers' => 'size:mid|color:red|' . $item[$lat_key] . ',' . $item[$lng_key],
        );
        if ($settings['marker_icon']) {
          $path = file_create_url($settings['marker_icon']);
          $query['markers'] = 'icon:' . $path . '|' . $query['markers'];
        }
        if (!empty($settings['api_key'])) {
          $query['key'] = $settings['api_key'];
        }
        $variables = array(
          'path' => url('//maps.google.com/maps/api/staticmap', array(
            'query' => $query,
            'external' => TRUE,
          )),
          'alt' => 'Geolocation',
          'attributes' => array(
            'class' => 'geolocation-googlemaps-static',
          ),
        );
        $map_img = theme('image', $variables);
        if ($settings['map_link'] === 1) {
          $map_link_query['q'] = $item[$lat_key] . ',' . $item[$lng_key];
          $element[$delta]['#markup'] = '<div>' . l($map_img, '//maps.google.com/maps', array(
            'query' => $map_link_query,
            'external' => TRUE,
            'html' => TRUE,
          )) . '</div>';
        }
        else {
          $element[$delta]['#markup'] = '<div>' . $map_img . '</div>';
        }
      }
      break;
    case 'geolocation_googlemaps_dynamic':
      $info = entity_get_info($entity_type);
      $key = isset($info['entity keys']['name']) ? $info['entity keys']['name'] : $info['entity keys']['id'];
      $eid = $entity->{$key};
      foreach ($items as $delta => $item) {
        $width = $settings['map_width'];
        $height = $settings['map_height'];
        $id = 'geolocation-googlemaps-dynamic-' . 'e_' . $eid . 'i_' . $instance['id'] . '-d_' . $delta;
        $map_element['googlemap'] = array(
          '#prefix' => '<div id="' . $id . '" class="geolocation-map geolocation-googlemaps-dynamic" ' . ' style="width:' . htmlentities($width) . ';height:' . htmlentities($height) . ';">',
          '#suffix' => '</div>',
        );

        // Attach CSS and JS files via FAPI '#attached'.
        $map_element['googlemap']['#attached']['css'][] = drupal_get_path('module', 'geolocation_googlemaps') . '/geolocation_googlemaps.css';
        $api_key = variable_get('geolocation_googlemaps_api_key', '');
        $data = array();
        if (!empty($api_key)) {
          $data['key'] = $api_key;
        }
        $query = drupal_http_build_query($data);
        $map_element['googlemap']['#attached']['js'][] = array(
          'data' => '//maps.google.com/maps/api/js?' . $query,
          'type' => 'external',
        );
        $map_element['googlemap']['#attached']['js'][] = array(
          'data' => '//www.google.com/jsapi',
          'type' => 'external',
        );
        $map_element['googlemap']['#attached']['js'][] = array(
          'data' => drupal_get_path('module', 'geolocation_googlemaps') . '/geolocation_googlemaps_dynamic_formatter.js',
          'type' => 'file',
          'scope' => 'footer',
        );

        // Create correct url for marker_icon.
        if ($settings['marker_icon']) {
          $settings['marker_icon'] = file_create_url($settings['marker_icon']);
        }

        // Add each delta to the settings array.
        $data = array(
          'formatters' => array(
            'e_' . $eid . 'i_' . $instance['id'] => array(
              "settings" => $settings,
              "deltas" => array(
                'd_' . $delta => array(
                  'lat' => $item[$lat_key],
                  'lng' => $item[$lng_key],
                ),
              ),
            ),
          ),
        );
        $map_element['googlemap']['#attached']['js'][] = array(
          'data' => array(
            'geolocationGooglemaps' => $data,
          ),
          'type' => 'setting',
        );
        $element[$delta] = $map_element;
      }
      break;
  }
  return $element;
}