You are here

function geofield_field_formatter_view in Geofield 7

Same name and namespace in other branches
  1. 7.2 geofield.formatters.inc \geofield_field_formatter_view()

Implements hook_field_formatter_view().

File

./geofield.formatters.inc, line 210
Drupal field formatter hooks and helper functions.

Code

function geofield_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  // Transform into centroid or bounding if needed
  if ($display['settings']['data'] != 'full') {
    geophp_load();
    if ($display['settings']['data'] == 'centroid') {
      foreach ($items as $delta => $item) {
        $centroid_wkt = 'POINT(' . $item['lon'] . ' ' . $item['lat'] . ')';
        $centroid = geoPHP::load($centroid_wkt, 'wkt');
        $items[$delta] = geofield_get_values_from_geometry($centroid);
      }
    }
    if ($display['settings']['data'] == 'bounding') {
      foreach ($items as $delta => $item) {
        $envelope_wkt = 'POLYGON ((' . $item['left'] . ' ' . $item['top'] . ', ' . $item['right'] . ' ' . $item['top'] . ', ' . $item['right'] . ' ' . $item['bottom'] . ', ' . $item['left'] . ' ' . $item['bottom'] . ', ' . $item['left'] . ' ' . $item['top'] . '))';
        $envelope = geoPHP::load($envelope_wkt, 'wkt');
        $items[$delta] = geofield_get_values_from_geometry($envelope);
      }
    }
  }

  // If we are a lat, lon, or latlon, and we are using degrees-minutes-seconds (instead of decimal degrees), then do a transformation
  if (isset($display['settings']['format'])) {
    if ($display['settings']['format'] == 'degrees_minutes_seconds') {
      foreach ($items as $delta => $item) {
        $items[$delta]['lat'] = geofield_latlon_DECtoDMS($item['lat'], 'lat');
        $items[$delta]['lon'] = geofield_latlon_DECtoDMS($item['lon'], 'lon');
      }
    }
  }

  // If we are a lat, lon, or latlon, and we are using celestial coordinate system (instead of decimal degrees), then do a transformation
  if (isset($display['settings']['format'])) {
    if ($display['settings']['format'] == 'ccs') {
      foreach ($items as $delta => $item) {
        $items[$delta]['lat'] = geofield_latlon_DECtoCCS($item['lat'], 'lat');
        $items[$delta]['lon'] = geofield_latlon_DECtoCCS($item['lon'], 'lon');
      }
    }
  }
  switch ($display['type']) {
    case 'geofield_wkt':
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#markup' => $item['wkt'],
        );
      }
      return $element;
    case 'geofield_geojson':
      geophp_load();
      foreach ($items as $delta => $item) {
        $geometry = geoPHP::load($item['wkt'], 'wkt');
        $json = $geometry
          ->out('json');
        $element[$delta] = array(
          '#markup' => $json,
        );
      }
      return $element;
    case 'geofield_kml':
      geophp_load();
      foreach ($items as $delta => $item) {
        $geometry = geoPHP::load($item['wkt'], 'wkt');
        $kml = $geometry
          ->out('kml');
        $element[$delta] = array(
          '#markup' => $kml,
        );
      }
      return $element;
    case 'geofield_gpx':
      geophp_load();
      foreach ($items as $delta => $item) {
        $geometry = geoPHP::load($item['wkt'], 'wkt');
        $kml = $geometry
          ->out('gpx');
        $element[$delta] = array(
          '#markup' => $kml,
        );
      }
      return $element;
    case 'geofield_latlon':
      foreach ($items as $delta => $item) {
        if ($display['settings']['labels']) {
          $output = t('Latitude: !latitude <br/>Longitude: !longitude', array(
            '!latitude' => $item['lat'],
            '!longitude' => $item['lon'],
          ));
        }
        else {
          $output = $item['lat'] . ', ' . $item['lon'];
        }
        $element[$delta] = array(
          '#markup' => $output,
        );
      }
      return $element;
    case 'geofield_lat':
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#markup' => $item['lat'],
        );
      }
      return $element;
    case 'geofield_lon':
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#markup' => $item['lon'],
        );
      }
      return $element;
    case 'geofield_geo_type':
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#markup' => $item['geo_type'],
        );
      }
      return $element;
    case 'geofield_openlayers':
      $map_name = $display['settings']['map_preset'] ? $display['settings']['map_preset'] : 'geofield_formatter_map';
      $element[0] = array(
        '#markup' => _geofield_openlayers_formatter($map_name, $items),
      );
      return $element;
    case 'geofield_def_list':
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#markup' => _geofield_def_list_formatter($item, $display['settings']),
        );
      }
      return $element;
    case 'geofield_description':
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#markup' => _geofield_description_formatter($item, $display['settings']),
        );
      }
      return $element;
  }
  return $element;
}