You are here

function getlocations_fields_field_formatter_view in Get Locations 7.2

Same name and namespace in other branches
  1. 7 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 604
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();
  $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();
  $element = array();
  $marker = $getlocations_defaults['map_marker'];
  $markertype = $entity_type . '_map_marker';
  if (isset($settings[$markertype]) && $settings[$markertype]) {
    $marker = $settings[$markertype];
  }
  else {
    $marker = $getlocations_defaults[$markertype];
  }
  $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};
  $locations = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array();

  // TODO markers, tested with node
  $bundle = FALSE;
  if (isset($entity->type)) {
    $bundle = $entity->type;
  }
  elseif (isset($entity_get_info['entity keys']['bundle']) && !empty($entity->{$entity_get_info['entity keys']['bundle']})) {
    $bundle = $entity->{$entity_get_info['entity keys']['bundle']};
  }
  $getlocations_markers = variable_get('getlocations_markers', array());
  if ($bundle && isset($getlocations_markers[$entity_type]['enable']) && $getlocations_markers[$entity_type]['enable']) {
    if (isset($getlocations_markers[$entity_type][$bundle][$field_name]['marker']) && $getlocations_markers[$entity_type][$bundle][$field_name]['marker']) {
      $marker = $getlocations_markers[$entity_type][$bundle][$field_name]['marker'];
    }
  }
  $settings['map_marker'] = $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;
}