You are here

function getlocations_leaflet_field_formatter_view in Get Locations 7

Same name and namespace in other branches
  1. 7.2 modules/getlocations_leaflet/getlocations_leaflet.module \getlocations_leaflet_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_leaflet/getlocations_leaflet.module, line 535
getlocations_leaflet.module @author Bob Hutchinson http://drupal.org/user/52366 @copyright GNU GPL

Code

function getlocations_leaflet_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  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_leaflet_map':
      if (empty($settings)) {
        $settings = getlocations_leaflet_map_formatter_defaults();
      }
      $module = getlocations_get_current_supported_module();
      $bundle = $instance['bundle'];
      $minmaxes = '';
      $default_settings = getlocations_leaflet_map_formatter_defaults();
      $default = getlocations_adjust_vars($default_settings, $settings);

      // awesome
      $marker = '';
      $vector = '';
      if ($settings['awesome'] && $settings['marker_type'] == 'fa') {
        if ($entity_type == 'node') {
          $per_field_markers = FALSE;
          $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) {
                      $mkey = 'node_marker__' . drupal_strtolower($type) . '__' . $field_name;
                      $vicon = getlocations_leaflet_awesome_marker_get($settings, $mkey);
                      $per_field_markers = TRUE;
                    }
                  }
                }
              }
            }
          }
          if (!$per_field_markers) {
            $vicon = getlocations_leaflet_awesome_marker_get($settings, 'node');
          }

          // TODO per tid marker
        }
        elseif ($entity_type == 'taxonomy_term') {
          $vicon = getlocations_leaflet_awesome_marker_get($settings, 'vocabulary');

          // TODO ??? sort out vocab/term
        }
        elseif ($entity_type == 'user') {
          $vicon = getlocations_leaflet_awesome_marker_get($settings, 'user');
        }
        elseif ($entity_type == 'comment') {
          $vicon = getlocations_leaflet_awesome_marker_get($settings, 'comment');
        }
        else {
          $vicon = getlocations_leaflet_awesome_marker_get($settings);
        }
        $vector = $vicon;
      }
      else {

        // marker
        if ($entity_type == 'node') {
          $per_field_markers = FALSE;
          $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'] : '';
                      $per_field_markers = TRUE;
                    }
                  }
                }
              }
            }
          }
          if (!$per_field_markers) {
            $marker = isset($default['node_map_marker']) ? $default['node_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'] : '';
        }
        else {
          $marker = isset($default['node_map_marker']) ? $default['node_map_marker'] : '';
        }
      }
      $mapzoom = FALSE;
      $iconlist = array();
      $minmaxes = array(
        'minlat' => 0,
        'minlon' => 0,
        'maxlat' => 0,
        'maxlon' => 0,
      );

      // fetch lats and lons
      $ct = 0;
      foreach (array_keys($locative_fields) as $delta) {
        $title = '';
        $lid = 0;
        if (isset($entity->{$locative_field_name}[$lang][$delta]) && ($location = $entity->{$locative_field_name}[$lang][$delta])) {

          // collect the lat and lon
          if ($module == 'getlocations_fields') {
            $lat = $location['latitude'];
            $lon = $location['longitude'];
            $lid = isset($location['glid']) ? $location['glid'] : 0;
          }
          elseif ($module == 'location_cck') {
            $lat = $location['latitude'];
            $lon = $location['longitude'];
            $lid = isset($location['lid']) ? $location['lid'] : 0;
          }
          elseif ($module == 'geofield') {
            $lat = $location['lat'];
            $lon = $location['lon'];
          }
          elseif ($module == 'geolocation') {
            $lat = $location['lat'];
            $lon = $location['lng'];
          }
          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 (isset($location['name']) && !empty($location['name'])) {
              $title = $location['name'];
            }
            else {
              $title = isset($entity->title) ? $entity->title : '';
            }
            $title = htmlspecialchars_decode(strip_tags($title), ENT_QUOTES);
            $minmaxes = getlocations_do_minmaxes($ct, $location, $minmaxes);

            // per item marker
            if (isset($location['marker']) && !empty($location['marker'])) {
              $marker = $location['marker'];
            }
            if (isset($location['mapzoom'])) {
              $mapzoom = $location['mapzoom'];
            }
            if ($lid && $default['markeraction'] == 'popup') {
              $extra = FALSE;
              if (isset($default['getdirections_link'])) {
                $extra['gdlink'] = $default['getdirections_link'];
              }
              $markeraction = array(
                'type' => 'popup',
                'data' => getlocations_getinfo($lid, $entity_key, $extra),
              );
            }
            elseif ($lid && $default['markeraction'] == 'link') {
              $markeraction = array(
                'type' => 'link',
                'data' => getlocations_getlidinfo($lid, $entity_key),
              );
            }
            else {
              $markeraction = array();
            }
            $iconlist[$marker] = getlocations_leaflet_get_marker($marker);
            $latlons[$ct] = array(
              $location['latitude'],
              $location['longitude'],
              $entity_key,
              $entity_id,
              $lid,
              $title,
              $marker,
              $vector,
              $markeraction,
              '',
            );
            $ct++;
          }

          // end lat lon check
        }
      }

      // end foreach
      if ($ct < 2) {
        unset($minmaxes);
        $minmaxes = '';
      }
      $map_settings = getlocations_leaflet_map_get_info('Getlocations OSM');
      $map_settings = getlocations_leaflet_get_map_layers($map_settings);
      if (!empty($settings['baselayers'])) {
        $map_layers = array();
        foreach ($settings['baselayers'] as $k => $v) {
          if (isset($map_settings['map_layers'][$k]) && $settings['baselayers'][$k]) {
            $map_layers[$k] = $map_settings['map_layers'][$k];
          }
        }
        if (!empty($map_layers)) {
          $map_settings['map_layers'] = $map_layers;
        }
      }
      $nodezoom = FALSE;
      if ($ct == 1 && $settings['nodezoom']) {
        $nodezoom = $settings['nodezoom'];
        $map_settings['map_opts']['center'] = array(
          $location['latitude'],
          $location['longitude'],
        );
        $map_settings['map_opts']['zoom'] = $nodezoom;
      }
      elseif ($ct < 1) {
        $ll = explode(',', $default['latlong']);
        $map_settings['map_opts']['center'] = array(
          $ll[0],
          $ll[1],
        );
        $map_settings['map_opts']['zoom'] = $default['zoom'];
      }
      $mapid = getlocations_setup_map('', FALSE, FALSE, TRUE);

      // add in some more
      $map_settings = getlocations_leaflet_map_settings_do($map_settings, $default, $latlons, $mapid);
      $map_settings['map_settings']['nodezoom'] = $nodezoom ? $nodezoom : $default['nodezoom'];
      $map_settings['map_settings']['minmaxes'] = empty($minmaxes) ? FALSE : implode(',', $minmaxes);
      $map_settings['icons'] = $iconlist;
      $map_settings['datanum'] = $ct;
      $map_settings['latlons'] = $latlons;
      $element[$delta] = array(
        '#theme' => 'getlocations_leaflet_map',
        '#width' => $default['width'],
        '#height' => $default['height'],
        '#map_settings' => $map_settings,
        '#mapid' => $mapid,
        '#links' => '',
      );
      break;
    case 'getlocations_leaflet_link':
      if (empty($settings) || !isset($settings['text'])) {
        $settings = getlocations_leaflet_link_formatter_defaults();
      }
      $bundle = $instance['bundle'];
      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_leaflet_link',
          '#link_text' => $link_text,
          '#entity_type' => $entity_type,
          '#entity_id' => $entity_id,
        );
      }
      break;
  }

  // end switch
  return $element;
}