You are here

function gm3_field_field_formatter_view in Google Maps API V3 7

Implements hook_field_formatter_view().

FIXME - Still need to do the field/entity formats properly (so that an entity map will show all the fields set as "entity_map" on that entity.

File

gm3_field/gm3_field.module, line 707

Code

function gm3_field_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  switch ($display['type']) {
    case 'gm3_entity_map':

      // Set an ID based on the entity.
      $entity_info = entity_get_info($entity_type);
      $id = "gm3_map-{$entity->{$entity_info['entity keys']['bundle']}}-{$entity->{$entity_info['entity keys']['id']}}";
      break;
    case 'gm3_field_map':

      // Setting the ID here is easy, as we can simply use the field ID.
      $id = $instance['field_name'];
      break;
    case 'gm3_text':
    default:
      $elements = array();
      switch ($field['type']) {
        case 'gm3_combination':
          foreach ($items as $item) {
            $elements[] = theme($item['gm3_type'] . '_text', array(
              'data' => $item,
            ));
          }
          break;
        default:
          foreach ($items as $item) {
            $elements[] = theme($field['type'] . '_text', array(
              'data' => $item,
            ));
          }
          break;
      }
      return $elements;
  }
  switch ($field['type']) {
    case 'gm3_point':
      if (count($items)) {

        // Set the items as not being editable.
        foreach ($items as $key => $item) {
          $items[$key]['editable'] = FALSE;
          $items[$key]['colour'] = variable_get('gm3_default_point_colour', 0);
        }
        module_load_include('theme.inc', 'gm3');
        $element = gm3_get_map(array(
          'map' => array(
            'id' => $id,
            'libraries' => array(
              'point' => array(
                'points' => $items,
                'convexhull' => $display['settings']['display_convex_hull'],
              ),
            ),
          ),
        ));
      }
      break;
    case 'gm3_rectangle':
    case 'gm3_polyline':
    case 'gm3_polygon':
      module_load_include('functions.inc', 'gm3');
      if (count($items)) {
        $polys = array();
        foreach ($items as $key => $item) {
          $array_to_pop = gm3_convert_polygon_string($item[substr($field['type'], 4)]);
          $polys[] = array(
            substr($field['type'], 4) => array_pop($array_to_pop),
            'editable' => FALSE,
          );
        }
        module_load_include('theme.inc', 'gm3');
        $element = gm3_get_map(array(
          'map' => array(
            'id' => $id,
            'libraries' => array(
              substr($field['type'], 4) => array(
                substr($field['type'], 4) . 's' => $polys,
              ),
            ),
          ),
        ));
      }
      break;
    case 'gm3_combination':
      $map = array(
        'id' => $id,
        'libraries' => array(
          'point' => array(
            'points' => array(),
            'convexhull' => $display['settings']['display_convex_hull'],
          ),
          'polygon' => array(
            'polygons' => array(),
          ),
          'polyline' => array(
            'polylines' => array(),
          ),
          'rectangle' => array(
            'rectangles' => array(),
          ),
        ),
      );
      $display_map = FALSE;
      module_load_include('functions.inc', 'gm3');
      foreach ($items as $item) {
        switch ($item['gm3_type']) {
          case 'rectangle':
          case 'polygon':
          case 'polyline':
            $shape = gm3_convert_polygon_string($item[$item['gm3_type']]);
            if (is_array($shape)) {
              $map['libraries'][$item['gm3_type']][$item['gm3_type'] . 's'][] = array(
                $item['gm3_type'] => array_pop($shape),
                'editable' => FALSE,
              );
              $display_map = TRUE;
            }
            break;
          case 'point':
            $map['libraries']['point']['points'][] = array(
              'latitude' => $item['latitude'],
              'longitude' => $item['longitude'],
              'colour' => variable_get('gm3_default_point_colour', 0),
              'editable' => FALSE,
            );
            $display_map = TRUE;
            break;
          default:
            if (function_exists("{$item['gm3_type']}_map_alter")) {
              $function = "{$item['gm3_type']}_map_alter";
              $function($map, $item);
              $display_map = TRUE;
            }
            break;
        }
      }
      if ($display_map) {
        module_load_include('theme.inc', 'gm3');
        $element = gm3_get_map(array(
          'map' => $map,
        ));
      }
      break;
    default:

      // Here we have a field that was not defined by this module, we use the
      // name of the module, plus the name of the type plus view to get the name
      // of the function to call
      // "{$field['module']}_{$field['type']}_view()"
      if (function_exists("{$field['module']}_{$field['type']}_view")) {
        $function_name = "{$field['module']}_{$field['type']}_view";
        $element = $function_name($entity_type, $entity, $field, $instance, $langcode, $items, $display, $id);
      }
      break;
  }
  if (isset($element) && $element) {
    return array(
      $element,
    );
  }
}