You are here

protected function ThemeService::setForecastsVariables in Weather 8

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

Adds variables related to weather forecast.

1 call to ThemeService::setForecastsVariables()
ThemeService::preprocessWeatherVariables in src/Service/ThemeService.php
Prepare variables to render weather display.

File

src/Service/ThemeService.php, line 154

Class

ThemeService
ThemeService service.

Namespace

Drupal\weather\Service

Code

protected function setForecastsVariables(&$variables, $idx, $weather, $display) {

  // Use a day counter to prepend "today" and "tomorrow" to forecast dates.
  $day_counter = 0;
  $forecasts = [];
  foreach ($weather['forecasts'] as $date => $time_ranges) {
    $formatted_date = $this->dateFormatter
      ->format(strtotime($date), 'weather_short');
    if ($day_counter == 0) {
      $formatted_date = $this
        ->t('Today, @date', [
        '@date' => $formatted_date,
      ]);
    }
    elseif ($day_counter == 1) {
      $formatted_date = $this
        ->t('Tomorrow, @date', [
        '@date' => $formatted_date,
      ]);
    }
    $forecasts[$date]['formatted_date'] = $formatted_date;

    // Calculate sunrise and sunset information, if desired.
    if ($display['show_sunrise_sunset']) {
      $forecasts[$date]['sun_info'] = $this
        ->calculateSunInfo($date, $variables['weather'][$idx]['utc_offset'], $variables['weather'][$idx]['geoid']);
    }
    foreach ($time_ranges as $time_range => $data) {
      $condition = $this
        ->formatCondition($data['symbol']);
      $forecasts[$date]['time_ranges'][$time_range]['condition'] = $condition;
      $forecasts[$date]['time_ranges'][$time_range]['symbol'] = $this
        ->formatImage($data['symbol'], $condition);
      $forecasts[$date]['time_ranges'][$time_range]['temperature'] = $this
        ->formatTemperature($data['temperature'], $display['temperature']);
      if ($display['show_windchill_temperature']) {
        $forecasts[$date]['time_ranges'][$time_range]['windchill'] = $this
          ->formatWindchillTemperature($data['temperature'], $data['wind_speed'], $display['temperature']);
      }
      $forecasts[$date]['time_ranges'][$time_range]['precipitation'] = $this
        ->t('@precipitation mm', [
        '@precipitation' => $data['precipitation'],
      ]);
      $forecasts[$date]['time_ranges'][$time_range]['pressure'] = $this
        ->formatPressure($data['pressure'], $display['pressure']);
      $forecasts[$date]['time_ranges'][$time_range]['wind'] = $this
        ->formatWind($data['wind_direction'], $data['wind_speed'], $display['windspeed'], $display['show_abbreviated_directions'], $display['show_directions_degree']);
    }
    $day_counter++;
  }
  $variables['weather'][$idx]['forecasts'] = $forecasts;
}