View source
<?php
function wunderground_weather_theme() {
return [
'wunderground_weather_forecast' => [
'variables' => [
'title' => NULL,
'data' => NULL,
'icon_set' => NULL,
'fields' => NULL,
'temperature_scale' => NULL,
'windspeed_scale' => NULL,
],
'function' => 'theme_wunderground_weather_forecast',
],
'wunderground_weather_current' => [
'variables' => [
'image' => NULL,
'summary' => NULL,
],
'template' => 'wunderground_weather_current',
],
];
}
function theme_wunderground_weather_forecast($variables) {
$wunderground_weather_manager = \Drupal::getContainer()
->get('wunderground_weather.manager');
$header = [];
$rows = [];
foreach ($variables['data'] as $i => $day) {
if (!empty($day) && is_numeric($i)) {
$header[] = [
'data' => $day->date->weekday . ' ' . $day->date->day . ' ' . $day->date->monthname,
];
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;
}
}
}
}
}
$table = [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
];
return render($table);
}