You are here

public function HelperService::getLinkForGeoid in Weather 8

Same name and namespace in other branches
  1. 2.0.x src/Service/HelperService.php \Drupal\weather\Service\HelperService::getLinkForGeoid()

Construct the link for the given GeoID.

Parameters

string $geoid: The GeoID to construct the link for.

string $destination: Destination to create the link for. Currently supported are 'system-wide', 'default', 'user', 'yr', and 'autocomplete'.

int $number: Optional number of display. Applies only to 'system-wide'.

Return value

string The link for the GeoID.

File

src/Service/HelperService.php, line 118

Class

HelperService
HelperService service.

Namespace

Drupal\weather\Service

Code

public function getLinkForGeoid($geoid, $destination, $number = 1) {
  $weatherPlace = $this->weatherPlaceStorage
    ->load($geoid);

  // Conversion rules for all link parts:
  // - Replace all spaces with an underscore.
  // - If the link part ends with a dot, use an underscore.
  $country = str_replace(' ', '_', $weatherPlace->country->value);
  if (substr($country, -1) == '.') {
    $country[strlen($country) - 1] = '_';
  }
  $link = $country . '/' . $weatherPlace->link->value;
  switch ($destination) {
    case 'system-wide':
      $link = '/weather/' . $link . '/' . $number;
      break;
    case 'default':
      $link = '/weather/' . $link;
      break;
    case 'user':
      $link = '/weather/' . $link . '/u';
      break;
    case 'yr':

      // Encode special characters except the '/' in the URL.
      // Otherwise, the request will fail on yr.no.
      $link = rawurlencode($link);
      $link = str_replace('%2F', '/', $link);
      $link = 'http://www.yr.no/place/' . $link . '/forecast.xml';
      break;
    case 'yr.no':
      $link = 'http://www.yr.no/place/' . $link . '/';
      break;
    case 'autocomplete':

      // Nothing to do here.
      break;
  }
  return $link;
}