You are here

wunderground_weather.module in Wunderground weather 8

Same filename and directory in other branches
  1. 7 wunderground_weather.module

File

wunderground_weather.module
View source
<?php

/**
 * Implements hook_theme().
 */
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',
    ],
  ];
}

/**
 * Theme function to render weather forecast block.
 *
 * @todo Remove and use render array instead.
 */
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);
}

Functions

Namesort descending Description
theme_wunderground_weather_forecast Theme function to render weather forecast block.
wunderground_weather_theme Implements hook_theme().