You are here

function getlocations_map_field_formatter_view in Get Locations 7

Same name and namespace in other branches
  1. 7.2 modules/getlocations_map/getlocations_map.module \getlocations_map_field_formatter_view()

Implements hook_field_formatter_view(). Build a renderable array for a field value.

Parameters

$entity_type: The type of $entity.

$entity: The entity being displayed.

$field: The field structure.

$instance: The field instance.

$langcode: The language associated with $items.

$items: Array of values for this field.

$display: The display settings to use, as found in the 'display' entry of instance definitions.

Return value

A renderable array for the $items, as an array of child elements keyed by numeric indexes starting from 0.

File

modules/getlocations_map/getlocations_map.module, line 105
getlocations_map.module @author Bob Hutchinson http://drupal.org/user/52366 @copyright GNU GPL

Code

function getlocations_map_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  drupal_add_css(GETLOCATIONS_MAP_PATH . '/getlocations_map.css');
  global $language;
  $lang = $langcode ? $langcode : ($entity->language ? $entity->language : $language->language);
  $locative_field_name = isset($items[0]['locative_field_name']) ? $items[0]['locative_field_name'] : '';
  if (empty($locative_field_name) || !isset($entity->{$locative_field_name}[$lang])) {
    return;
  }
  $locative_fields = $entity->{$locative_field_name}[$lang];
  if (!(is_array($locative_fields) && count($locative_fields) > 0)) {
    return;
  }
  $settings = $display['settings'];
  $element = array();
  switch ($display['type']) {
    case 'getlocations_map_default':
      $module = getlocations_get_current_supported_module();
      $bundle = $instance['bundle'];
      $minmaxes = '';

      // marker
      $marker = '';
      if (empty($settings)) {
        $settings = getlocations_map_map_formatter_defaults();
      }
      $default_settings = getlocations_map_map_formatter_defaults();
      $default = getlocations_adjust_vars($default_settings, $settings);
      $marker = isset($default['node_map_marker']) ? $default['node_map_marker'] : '';
      if ($entity_type == 'node') {
        $getlocations_node_marker = variable_get('getlocations_node_marker', array(
          'enable' => 0,
        ));
        if ($getlocations_node_marker['enable']) {
          if ($types = getlocations_get_types()) {
            foreach ($types as $type => $name) {
              if ($type == $bundle) {
                $field_names = getlocations_get_fieldname2($type, 'node');
                foreach ($field_names as $field_name) {
                  if ($field_name == $locative_field_name) {
                    $marker = isset($getlocations_node_marker['content_type'][$type]['field_name'][$field_name]['map_marker']) ? $getlocations_node_marker['content_type'][$type]['field_name'][$field_name]['map_marker'] : '';
                  }
                }
              }
            }
          }
        }
      }
      elseif ($entity_type == 'user') {
        $marker = isset($default['user_map_marker']) ? $default['user_map_marker'] : '';
      }
      elseif ($entity_type == 'taxonomy_term') {

        // TODO needs testing

        //////
        if (module_exists('taxonomy')) {

          // vocabulary markers
          if ($types = getlocations_get_vocabularies()) {
            $getlocations_vocabulary_marker = variable_get('getlocations_vocabulary_marker', array(
              'enable' => 0,
            ));
            if ($getlocations_vocabulary_marker['enable']) {
              foreach ($types as $type => $name) {
                $field_name = getlocations_get_fieldname($type, 'taxonomy_term');
                if ($field_name == $locative_field_name) {
                  $marker = isset($getlocations_vocabulary_marker['vocabulary'][$type]['map_marker']) ? $getlocations_vocabulary_marker['vocabulary'][$type]['map_marker'] : $default['vocabulary_map_marker'];
                }
              }
            }
          }

          // term markers
          $getlocations_term_marker = variable_get('getlocations_term_marker', array(
            'enable' => 0,
            'vids' => 0,
            'max_depth' => '',
          ));
          if ($getlocations_term_marker['enable'] && $getlocations_term_marker['vids']) {
            $depth = is_numeric($getlocations_term_marker['max_depth']) && $getlocations_term_marker['max_depth'] > 0 ? $getlocations_term_marker['max_depth'] : NULL;
            $vids = $getlocations_term_marker['vids'];
            foreach ($vids as $vid) {
              $terms = taxonomy_get_tree($vid, 0, $depth);
              foreach ($terms as $term) {
                $tid = $term->tid;
                $marker = isset($getlocations_term_marker['vid'][$vid]['term'][$tid]['map_marker']) ? $getlocations_term_marker['vid'][$vid]['term'][$tid]['map_marker'] : $default['term_map_marker'];
              }
            }
          }
        }

        //////
      }
      elseif ($entity_type == 'comment') {
        $marker = isset($default['comment_map_marker']) ? $default['comment_map_marker'] : '';
      }

      // fetch lats and lons
      $ct = 0;
      foreach (array_keys($locative_fields) as $delta) {
        $title = '';
        if ($module == 'getlocations_fields') {
          if (isset($entity->{$locative_field_name}[$lang][$delta])) {
            if ($location = $entity->{$locative_field_name}[$lang][$delta]) {
              $lat = $location['latitude'];
              $lon = $location['longitude'];

              // per item marker
              if (isset($location['marker']) && !empty($location['marker'])) {
                $marker = $location['marker'];
              }
              if (isset($location['name']) && !empty($location['name'])) {
                $title = $location['name'];
              }
            }
          }

          // smuggle these in from location data
          // streetview_ settings
          if (isset($location['streetview_settings_allow'])) {
            $streetview_settings_allow = $location['streetview_settings_allow'];
          }
          else {
            $streetview_settings_allow = getlocations_fields_streetview_settings_allow();
          }
          if ($streetview_settings_allow) {
            if (isset($location['sv_heading']) && is_numeric($location['sv_heading'])) {
              $default['sv_heading'] = $location['sv_heading'];
            }
            if (isset($location['sv_zoom']) && is_numeric($location['sv_zoom'])) {
              $default['sv_zoom'] = $location['sv_zoom'];
            }
            if (isset($location['sv_pitch']) && is_numeric($location['sv_pitch'])) {
              $default['sv_pitch'] = $location['sv_pitch'];
            }
            if (isset($location['sv_showfirst']) && is_numeric($location['sv_showfirst'])) {
              $default['sv_showfirst'] = $location['sv_showfirst'];
            }

            // streetview display settings
            $display_settings_defaults = getlocations_fields_get_display_settings_defaults();
            foreach ($display_settings_defaults as $dkey => $dval) {
              if (isset($location[$dkey])) {
                $default[$dkey] = $location[$dkey];
              }
              else {
                $default[$dkey] = $dval;
              }
            }
          }

          // map_settings
          if (isset($location['map_settings_allow'])) {
            $map_settings_allow = $location['map_settings_allow'];
          }
          else {
            $map_settings_allow = getlocations_fields_map_settings_allow();
          }
          if ($map_settings_allow) {
            if (isset($location['mapzoom']) && is_numeric($location['mapzoom'])) {
              $default['nodezoom'] = $location['mapzoom'];
            }
            if (isset($location['map_maptype']) && $location['map_maptype']) {
              $default['maptype'] = $location['map_maptype'];
            }

            // update baselayers
            $default['baselayers'][$location['map_maptype']] = 1;
          }
        }
        elseif ($module == 'location_cck') {
          if (isset($entity->{$locative_field_name}[$lang][$delta])) {
            if ($location = $entity->{$locative_field_name}[$lang][$delta]) {
              $lat = $location['latitude'];
              $lon = $location['longitude'];
              if (isset($location['name']) && !empty($location['name'])) {
                $title = $location['name'];
              }
            }
          }
        }
        elseif ($module == 'geofield') {
          if ($location = $entity->{$locative_field_name}[$lang][$delta]) {
            $lat = $location['lat'];
            $lon = $location['lon'];
          }
        }
        elseif ($module == 'geolocation') {
          if ($location = $entity->{$locative_field_name}[$lang][$delta]) {
            $lat = $location['lat'];
            $lon = $location['lng'];
          }
        }

        # elseif other modules here
        if (isset($lat) && !empty($lat) && isset($lon) && !empty($lon) && ($latlon = getlocations_latlon_check($lat . ',' . $lon))) {
          $ll = explode(',', $latlon);
          $location['latitude'] = $ll[0];
          $location['longitude'] = $ll[1];
          $entity_get_info = entity_get_info($entity_type);
          $entity_key = $entity_get_info['entity keys']['id'];

          // nid, cid, uid etc
          $entity_id = $entity->{$entity_key};
          if (empty($title)) {
            $title = isset($entity->title) ? $entity->title : '';
          }
          $title = htmlspecialchars_decode(strip_tags($title), ENT_QUOTES);
          $minmaxes = getlocations_do_minmaxes($ct, $location, $minmaxes);
          $latlons[] = array(
            $lat,
            $lon,
            isset($location['lid']) ? $location['lid'] : $entity_id,
            $title,
            $marker,
            $entity_key,
            '',
            '',
          );
          $ct++;
        }

        // end lat lon check
      }

      // end foreach
      $mapid = getlocations_setup_map($default);
      if ($ct < 2) {
        unset($minmaxes);
        $minmaxes = '';
      }
      getlocations_js_settings_do($default, $latlons, $minmaxes, $mapid);
      $element[$delta] = array(
        '#theme' => 'getlocations_show',
        '#width' => $default['width'],
        '#height' => $default['height'],
        '#defaults' => $default,
        '#mapid' => $mapid,
        '#latlons' => $latlons,
        '#minmaxes' => $minmaxes,
        '#type' => '',
        '#node' => '',
      );
      break;
    case 'getlocations_map_link':
      if (empty($settings) || !isset($settings['text'])) {
        $settings = getlocations_map_link_formatter_defaults();
      }
      foreach (array_keys($locative_fields) as $delta) {
        $entity_get_info = entity_get_info($entity_type);
        $entity_key = $entity_get_info['entity keys']['id'];

        // nid, cid, uid etc
        $entity_id = $entity->{$entity_key};
        $link_text = $settings['text'];
        if ($settings['text_opt'] == 'page') {
          $entity_title = '';
          if (isset($entity_get_info['entity keys']['label'])) {
            $entity_title = $entity_get_info['entity keys']['label'];
          }
          elseif ($entity_type == 'user') {
            $entity_title = 'name';
          }
          if ($entity_title && isset($entity->{$entity_title})) {
            $link_text = $entity->{$entity_title};
          }
        }
        $element[$delta] = array(
          '#theme' => 'getlocations_map_link',
          '#link_text' => $link_text,
          '#entity_type' => $entity_type,
          '#entity_id' => $entity_id,
        );
      }
      break;
  }

  // end switch
  return $element;
}