You are here

function theme_wunderground_weather_forecast in Wunderground weather 8

Same name and namespace in other branches
  1. 7 wunderground_weather.module \theme_wunderground_weather_forecast()

Theme function to render weather forecast block.

@todo Remove and use render array instead.

1 string reference to 'theme_wunderground_weather_forecast'
wunderground_weather_theme in ./wunderground_weather.module
Implements hook_theme().

File

./wunderground_weather.module, line 34

Code

function theme_wunderground_weather_forecast($variables) {

  /** @var \Drupal\wunderground_weather\WundergroundWeatherManager $wunderground_weather_manager */
  $wunderground_weather_manager = \Drupal::getContainer()
    ->get('wunderground_weather.manager');
  $header = [];
  $rows = [];
  foreach ($variables['data'] as $i => $day) {
    if (!empty($day) && is_numeric($i)) {

      // Table header.
      $header[] = [
        'data' => $day->date->weekday . ' ' . $day->date->day . ' ' . $day->date->monthname,
      ];

      // Build rows.
      foreach ($variables['fields'] as $field => $display) {
        if ($display) {
          switch ($field) {
            case 'image':
              $description = t('Weather forecast for @date', [
                '@date' => $day->date->weekday . ' ' . $day->date->day . ' ' . $day->date->monthname,
              ]);
              $uri = $wunderground_weather_manager
                ->getIconUrl($variables['icon_set'], $day->icon);
              $image = [
                '#theme' => 'image',
                '#uri' => $uri,
                '#alt' => $description,
                '#stitle' => $description,
              ];
              $rows[$field][] = render($image);
              break;
            case 'temperature':
              $scale = $variables['temperature_scale'];
              if ($scale === 'c') {
                $high = $day->high->celsius;
                $low = $day->low->celsius;
              }
              else {
                $high = $day->high->fahrenheit;
                $low = $day->low->fahrenheit;
              }
              $temperature = $high . '°' . strtoupper($scale);
              $temperature .= ' / ';
              $temperature .= $low . '°' . strtoupper($scale);
              $rows[$field][] = $temperature;
              break;
            case 'conditions':
              $rows[$field][] = $day->conditions;
              break;
            case 'rain':
              $rows[$field][] = $day->pop . '% ' . t('rain');
              break;
            case 'wind':
              $windspeed_scale = $variables['windspeed_scale'];
              switch ($windspeed_scale) {
                case 'mph':
                  $windspeed = $day->avewind->mph;
                  break;
                case 'kph':
                  $windspeed = $day->avewind->kph;
                  break;
                default:
                  $wind_kph = $day->avewind->kph;
                  $windspeed = $wunderground_weather_manager
                    ->windSpeedToBeaufort($wind_kph, 'kph');
                  break;
              }
              $rows[$field][] = $windspeed . ' ' . $windspeed_scale;
              break;
          }
        }
      }
    }
  }

  // Variables for hook_table.
  $table = [
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
  ];

  // Return a table.
  return render($table);
}