public function LiveWeatherBlock::build in Live Weather 8.2
Same name and namespace in other branches
- 8 src/Plugin/Block/LiveWeatherBlock.php \Drupal\live_weather\Plugin\Block\LiveWeatherBlock::build()
Builds and returns the renderable array for this block plugin.
If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).
Return value
array A renderable array representing the content of the block.
Overrides BlockPluginInterface::build
See also
\Drupal\block\BlockViewBuilder
File
- src/
Plugin/ Block/ LiveWeatherBlock.php, line 103 - Contains \Drupal\live_weather\Plugin\Block\LiveWeatherBlock.
Class
- LiveWeatherBlock
- Provides a 'Live Weather' block.
Namespace
Drupal\live_weather\Plugin\BlockCode
public function build() {
$config = $this->configuration['list'];
$location_list = $this->configFactory
->get('live_weather.location')
->get('location');
$location_list = array_keys($location_list);
$location_list = array_intersect($config['list'], $location_list);
$settings = $this->configFactory
->get('live_weather.settings')
->get('settings');
$html = array();
$feed = array();
$feed_data = $this->liveWeather;
$i = 0;
if (!empty($location_list)) {
foreach ($location_list as $woeid) {
$data = $feed_data
->locationCheck($woeid, ' * ', $settings['unit']);
if (is_array($data) && !empty($data)) {
if (!empty($data['location']['city'])) {
$temp = Html::escape($data['current_observation']['condition']['temperature']);
$date = Html::escape($data['current_observation']['pubDate']);
$feed_sunrise = Html::escape($data['current_observation']['astronomy']['sunrise']);
$feed_sunset = Html::escape($data['current_observation']['astronomy']['sunset']);
$daynight = $feed_data
->checkDayNight($date, $feed_sunrise, $feed_sunset);
$wind_direction = $feed_data
->windDirection(Html::escape($data['current_observation']['wind']['direction']));
$html[$i]['location'] = Html::escape($data['location']['city']) . ', ' . Html::escape($data['location']['region']) . ', ' . Html::escape($data['location']['country']);
$html[$i]['temperature'] = $settings['unit'] == 'C' ? round(($temp - 32) * 5 / 9) : $temp;
$html[$i]['temperature_unit'] = $settings['unit'];
$html[$i]['text'] = Html::escape($data['current_observation']['condition']['text']);
if ($settings['image']) {
$html[$i]['image'] = 'https://s.yimg.com/zz/combo?a/i/us/nws/weather/gr/' . Html::escape($data['current_observation']['condition']['code']) . $daynight;
}
if ($settings['wind']) {
$html[$i]['wind'] = Html::escape($data['current_observation']['wind']['speed']) . ' mph ' . $wind_direction;
}
if ($settings['humidity']) {
$html[$i]['humidity'] = Html::escape($data['current_observation']['atmosphere']['humidity']);
}
if ($settings['visibility']) {
$html[$i]['visibility'] = Html::escape($data['current_observation']['atmosphere']['visibility']);
}
if ($settings['sunrise']) {
$html[$i]['sunrise'] = $feed_sunrise;
}
if ($settings['sunset']) {
$html[$i]['sunset'] = $feed_sunset;
}
}
$i++;
}
}
}
return array(
'#theme' => 'live_weather',
'#weather_detail' => $html,
'#cache' => array(
'max-age' => $settings['cache'],
),
);
}