You are here

function getdirections_fields_field_formatter_view in Get Directions 7.3

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/getdirections_fields/getdirections_fields.module, line 100
Provides Field module integration for Getdirections

Code

function getdirections_fields_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  global $language;
  $lang = $langcode ? $langcode : ($entity->language ? $entity->language : $language->language);
  $settings = $display['settings'];
  $module = getdirections_get_current_supported_module();
  $address_field_name = FALSE;
  $loc = FALSE;
  $element = array();
  foreach ($items as $delta => $item) {
    $locative_field_name = $item['locative_field_name'];
    if ($locative_field_name) {
      if ($module == 'getlocations_fields') {
        if (isset($entity->{$locative_field_name}[$lang][$delta])) {
          if ($location = $entity->{$locative_field_name}[$lang][$delta]) {
            $latlon = $location['latitude'] . ',' . $location['longitude'];
            $loc = _getdirections_loadaddress($location);
            if (!$loc) {
              $loc = t('This address');
            }
          }
        }
      }
      elseif ($module == 'location_cck' || $module == 'location_node') {

        // NOT TESTED
        if (isset($entity->{$locative_field_name}[$lang][$delta])) {
          if ($location = $entity->{$locative_field_name}[$lang][$delta]) {
            $latlon = $location['latitude'] . ',' . $location['longitude'];
            $loc = _getdirections_loadaddress($location);
            if (!$loc) {
              $loc = t('This address');
            }
          }
        }
      }
      elseif ($module == 'geofield') {
        if ($location = $entity->{$locative_field_name}[$lang][$delta]) {
          $latlon = $location['lat'] . ',' . $location['lon'];
          $address_field_name = $item['address_field_name'];
          if ($address_field_name && isset($entity->{$address_field_name}[$lang][$delta])) {
            if ($addressfield = $entity->{$address_field_name}[$lang][$delta]) {
              $loc = _getdirections_addressfield_loadaddress($addressfield);
            }
          }
          if (!$loc) {
            $loc = t('This address');
          }
        }
      }
      elseif ($module == 'geolocation') {

        // NOT TESTED
        if ($location = $entity->{$locative_field_name}[$lang][$delta]) {
          $latlon = $location['lat'] . ',' . $location['lng'];
          $address_field_name = $item['address_field_name'];
          if ($address_field_name && isset($entity->{$address_field_name}[$lang][$delta])) {
            if ($addressfield = $entity->{$address_field_name}[$lang][$delta]) {
              $loc = _getdirections_addressfield_loadaddress($addressfield);
            }
          }
          if (!$loc) {
            $loc = t('This address');
          }
        }
      }

      # elseif other modules here
    }
    if (isset($latlon) && !empty($latlon) && getdirections_latlon_check($latlon)) {
      $element = array();
      switch ($display['type']) {
        case 'getdirections_fields_default':
          if (empty($settings)) {
            $settings = getdirections_fields_field_formatter_settings();
          }
          $default_settings = getdirections_fields_field_formatter_settings();
          $default = getdirections_adjust_vars($default_settings['default'], $settings['default']);
          $misc = getdirections_adjust_vars($default_settings['misc'], $settings['misc']);
          $settings['misc'] = $misc;
          $settings['default'] = $default;
          getdirections_setup_map($default, $misc);
          $getdirections_settings = getdirections_get_settings($default, $misc);
          $getdirections_settings['tolatlon'] = $latlon;
          $mapid = getdirections_get_key();
          drupal_add_js(array(
            'getdirections' => array(
              $mapid => $getdirections_settings,
            ),
          ), 'setting');
          $direction_opt = isset($settings['direction_opt']) ? $settings['direction_opt'] : 'to';
          $rawform = drupal_get_form('getdirections_fields_direction_form', $default, $misc, $mapid, $direction_opt, $loc, '', $latlon);
          $form = drupal_render($rawform);
          $element[$delta] = array(
            '#theme' => 'getdirections_fields_show',
            '#form' => $form,
            '#settings' => $settings,
            '#mapid' => $mapid,
          );
          break;
        case 'getdirections_fields_link':
          $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};
            }
          }
          elseif ($settings['text_opt'] == 'address') {
            $link_text = $loc;
          }
          $element[$delta] = array(
            '#theme' => 'getdirections_fields_link',
            '#link_text' => $link_text,
            '#entity_type' => $entity_type,
            '#entity_id' => $entity_id,
            '#dir' => $settings['direction_opt'],
          );
          break;
      }
    }
  }
  return $element;
}