You are here

private function ip_geoloc_plugin_style_leaflet::_fill_out_location_region in IP Geolocation Views & Maps 7

Fills out the region hierarchy belonging to a location object.

Parameters

string $field_type: 'taxonomy_term_reference', 'addressfield' or other

object $location: The location object whose regions attribute will be fleshed ut

string $region: Region or region hierarchy taken from the View result in the form of a taxonomy term (leaf) or AddressField. Or call the function repeatedly on the same location object passing in regions as plain fields, one by one, going from the big region (country) down to the small (suburb)

int $level: Level of the region in the hierarchy, updated on return

1 call to ip_geoloc_plugin_style_leaflet::_fill_out_location_region()
ip_geoloc_plugin_style_leaflet::fill_out_location_regions in views/ip_geoloc_plugin_style_leaflet.inc

File

views/ip_geoloc_plugin_style_leaflet.inc, line 1651

Class

ip_geoloc_plugin_style_leaflet

Code

private function _fill_out_location_region($field_type, &$location, $region, &$level) {
  switch ($field_type) {
    case 'taxonomy_term_reference':
      $region_hierarchy = taxonomy_get_parents_all($region);

      // Reverse, to order region hierarchy from large region to small.
      foreach (array_reverse($region_hierarchy) as $region_term) {
        $location->regions[$level++] = trim($region_term->name);
      }
      break;
    case 'addressfield':

      //$region = reset($region);
      if (!empty($region)) {
        $format_callback = 'addressfield_format_address_generate';
        if (function_exists($format_callback) && isset($region['country'])) {
          $format = array();
          $context = array(
            'mode' => NULL,
          );

          // Replace state and country codes by their full names.
          addressfield_format_address_generate($format, $region, $context);
          if (isset($format['country']['#options'][$region['country']])) {
            $region['country'] = $format['country']['#options'][$region['country']];
          }
          if (isset($region['administrative_area']) && isset($format['locality_block']['administrative_area']['#options'][$region['administrative_area']])) {
            $region['administrative_area'] = $format['locality_block']['administrative_area']['#options'][$region['administrative_area']];
          }
        }
        else {

          //drupal_set_message(t('IPGV&M: cannot flesh out countries and states on locations. Format callback %name is not available.', array('%name' => $format_callback)), 'warning', FALSE);
        }
        $location->regions = array(
          1 => isset($region['country']) ? trim($region['country']) : '',
          2 => isset($region['administrative_area']) ? trim($region['administrative_area']) : '',
          3 => isset($region['locality']) ? trim($region['locality']) : '',
          4 => isset($region['postal_code']) ? trim($region['postal_code']) : '',
        );
        $level = 5;
      }
      break;
    default:

      // Note: $location->regions is meant to be ordered big to small
      $location->regions[$level++] = trim($region);
  }
}