You are here

private function IpGeoLocPluginStyleLeaflet::fillOutLocationRegion in IP Geolocation Views & Maps 8

Fills out the region hierarchy belonging to a location object.

Parameters

string $field_type: Is a '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 IpGeoLocPluginStyleLeaflet::fillOutLocationRegion()
IpGeoLocPluginStyleLeaflet::fillOutLocationRegions in src/Plugin/views/style/IpGeoLocPluginStyleLeaflet.php
Pending doc.

File

src/Plugin/views/style/IpGeoLocPluginStyleLeaflet.php, line 1183

Class

IpGeoLocPluginStyleLeaflet
Views Style plugin extension for Leaflet (if enabled).

Namespace

Drupal\ip_geoloc\Plugin\views\style

Code

private function fillOutLocationRegion($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 = [];
          $context = [
            '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 = [
          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);
  }
}