You are here

public function WeatherDetailedForecastController::detailedForecast in Weather 8

Same name and namespace in other branches
  1. 2.0.x src/Controller/WeatherDetailedForecastController.php \Drupal\weather\Controller\WeatherDetailedForecastController::detailedForecast()

Builds the response for weather.detailed_forecast route.

1 string reference to 'WeatherDetailedForecastController::detailedForecast'
weather.routing.yml in ./weather.routing.yml
weather.routing.yml

File

src/Controller/WeatherDetailedForecastController.php, line 61

Class

WeatherDetailedForecastController
Returns responses for Weather routes.

Namespace

Drupal\weather\Controller

Code

public function detailedForecast(string $country, string $place, string $city, string $destination) {
  $link = $place . '/' . $city;
  $weatherPlace = $this->weatherPlaceStorage
    ->getQuery()
    ->condition('country', $country, 'LIKE')
    ->condition('link', $link, 'LIKE')
    ->execute();
  if ($weatherPlace) {
    $weatherPlace = $this->weatherPlaceStorage
      ->load(reset($weatherPlace));
  }

  // If the last part of the link contains an appended slash
  // and a number, this indicates the display configuration of
  // the system-wide display with the given number.
  $display_type = 'default';
  $display_number = 0;

  // Examine the last element of the link.
  if (preg_match('/^[0-9]+$/', $destination)) {

    // Use the system-wide display with the given number.
    $display_type = 'system-wide';
    $display_number = $destination;
  }
  elseif ($destination == 'u') {

    // Use the user's custom display.
    $display_type = 'user';
    $display_number = $this
      ->currentUser()
      ->id();
  }
  if ($weatherPlace instanceof WeatherPlaceInterface) {

    // Show detailed forecast only if Weather Place
    // was configured for the Display.
    $configured = $this->displayPlaceStorage
      ->getQuery()
      ->condition('geoid', $weatherPlace
      ->id())
      ->condition('display_type', $display_type)
      ->condition('display_number', $display_number)
      ->execute();
    if ($configured) {
      return [
        '#theme' => 'weather_detailed_forecast',
        '#weather_display_place' => $this->displayPlaceStorage
          ->load(reset($configured)),
        '#display_type' => $display_type,
        '#display_number' => $display_number,
      ];
    }
  }
  throw new NotFoundHttpException();
}