You are here

function getlocations_fields_field_formatter_view in Get Locations 7

Same name and namespace in other branches
  1. 7.2 modules/getlocations_fields/getlocations_fields.module \getlocations_fields_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.

See also

getlocations_fields_field_formatter_info()

File

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

Code

function getlocations_fields_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  drupal_add_css(GETLOCATIONS_FIELDS_PATH . '/getlocations_fields.css');
  $settings = $display['settings'];
  if (empty($settings)) {
    $settings = getlocations_fields_field_formatter_info_defaults();
  }

  // reset markers, in case there are any left from old method
  $getlocations_defaults = getlocations_defaults();
  $settings['node_map_marker'] = $getlocations_defaults['node_map_marker'];
  $settings['user_map_marker'] = $getlocations_defaults['user_map_marker'];
  $settings['vocabulary_map_marker'] = $getlocations_defaults['vocabulary_map_marker'];
  $settings['comment_map_marker'] = $getlocations_defaults['comment_map_marker'];
  $settings['term_map_marker'] = $getlocations_defaults['term_map_marker'];
  $field_name = '';
  if (isset($field['field_name'])) {
    $field_name = $field['field_name'];
  }
  if (empty($field_name) || !isset($entity->{$field_name}) || empty($entity->{$field_name})) {
    return;
  }
  $locations = array();
  $supported_entity_types = array(
    'node',
    'user',
    'taxonomy_term',
    'comment',
    'profile2',
  );
  $element = array();
  if (!in_array($entity_type, $supported_entity_types)) {
    return $element;
  }
  if ($entity_type == 'node') {
    $locations = getlocations_fields_load_locations($entity->vid, 'vid', $field_name);
    if (count($locations)) {
      $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) {
            $field_names = getlocations_get_fieldname2($type, $entity_type);
            foreach ($field_names as $f_name) {
              if ($field_name && $f_name == $field_name) {
                $mkey = 'node_marker__' . strtolower($type) . '__' . $field_name;
                if (isset($settings[$mkey])) {
                  $settings['map_marker'] = $settings[$mkey];
                }
                else {
                  $settings['map_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'] : $settings['node_map_marker'];
                }
              }
            }
          }
        }
      }
      else {
        $settings['map_marker'] = $settings['node_map_marker'];
      }

      // term marker. this will override node marker if there is one
      $settings['map_marker'] = getlocations_get_term_marker($entity->nid, $settings['map_marker']);
    }
  }
  elseif ($entity_type == 'taxonomy_term' && module_exists('taxonomy')) {
    $locations = getlocations_fields_load_locations($entity->tid, 'tid', $field_name);
    if (count($locations)) {
      $settings['map_marker'] = $settings['vocabulary_map_marker'];
      $getlocations_vocabulary_marker = variable_get('getlocations_vocabulary_marker', array(
        'enable' => 0,
      ));
      if ($getlocations_vocabulary_marker['enable']) {
        if ($types = getlocations_get_vocabularies()) {
          foreach ($types as $type => $name) {
            $f_name = getlocations_get_fieldname($type, $entity_type);
            if ($f_name == $field_name) {
              $mkey = 'vocabulary_marker_' . $field_name;
              if (isset($settings[$mkey])) {
                $settings['map_marker'] = $settings[$mkey];
              }
              else {
                $settings['map_marker'] = isset($getlocations_vocabulary_marker['vocabulary'][$type]['map_marker']) ? $getlocations_vocabulary_marker['vocabulary'][$type]['map_marker'] : $settings['vocabulary_map_marker'];
              }
            }
          }
        }
      }
      else {
        $settings['map_marker'] = $settings['vocabulary_map_marker'];
      }
    }
  }
  elseif ($entity_type == 'user' || $entity_type == 'profile2') {
    $locations = getlocations_fields_load_locations($entity->uid, 'uid', $field_name);
    if (count($locations)) {
      $settings['map_marker'] = $settings['user_map_marker'];
    }
  }
  elseif ($entity_type == 'comment') {
    $locations = getlocations_fields_load_locations($entity->cid, 'cid', $field_name);
    if (count($locations)) {
      $settings['map_marker'] = $settings['comment_map_marker'];
    }
  }
  switch ($display['type']) {
    case 'getlocations_fields_default':
      if (count($locations)) {

        #      foreach ($items as $delta => $item) {

        #        $element[$delta] = array(

        #          '#theme' => 'getlocations_fields_show',

        #          '#locations' => $locations,

        #          '#settings' => $settings,

        #        );

        #      }
        $element[0] = array(
          '#theme' => 'getlocations_fields_show',
          '#locations' => $locations,
          '#settings' => $settings,
        );
      }
  }
  return $element;
}