public function WeatherCurrentBlock::build in Wunderground weather 8
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/ WeatherCurrentBlock.php, line 159 - Contains \Drupal\wunderground_weather\Plugin\Block\WeatherCurrentBlock.
Class
- WeatherCurrentBlock
- Provides a block with current weather conditions.
Namespace
Drupal\wunderground_weather\Plugin\BlockCode
public function build() {
// Get block configuration.
$config = $this
->getConfiguration();
$location = $config['location_current'];
$icon_set = $config['icon_set'];
$temperature_scale = $config['temperature_scale'];
$windspeed_scale = $config['windspeed_scale'];
// Get all settings.
$settings = $this->wundergroundWeatherManager
->getSettings();
preg_match('#\\[(.*?)\\]#', $location, $match);
$path = $match[1];
$options = [
'api' => 'api',
'key' => $settings
->get('api_key'),
'data_feature' => 'conditions',
'language' => 'lang:' . strtoupper($settings
->get('language')),
'path' => $path,
];
// Check if data is received.
if ($weather = $this->wundergroundWeatherManager
->requestData($options)) {
// Get fields to be displayed.
$fields = $this->configuration['current_fields'];
// Build list items.
$items = [];
foreach ($fields as $field => $display) {
if ($display && isset($weather->current_observation)) {
// Calculate windspeed.
switch ($windspeed_scale) {
case 'mph':
$windspeed = $weather->current_observation->wind_mph;
break;
case 'kph':
$windspeed = $weather->current_observation->wind_kph;
break;
default:
$wind_kph = $weather->current_observation->wind_kph;
$windspeed = $this->wundergroundWeatherManager
->windSpeedToBeaufort($wind_kph, 'kph');
break;
}
switch ($field) {
case 'weather':
$items[$field] = $weather->current_observation->weather;
break;
case 'temperature':
$temperature_property = 'temp_' . $temperature_scale;
$items[$field] = t('Temperature: @temp °@scale', [
'@temp' => $weather->current_observation->{$temperature_property},
'@scale' => strtoupper($temperature_scale),
]);
break;
case 'feels_like':
$temperature_property = 'feelslike_' . $temperature_scale;
$items[$field] = t('Feels like: @temp °@scale', [
'@temp' => $weather->current_observation->{$temperature_property},
'@scale' => strtoupper($temperature_scale),
]);
break;
case 'wind':
$items[$field] = t('Wind');
$items[$field] .= ': ' . $windspeed . ' ' . $windspeed_scale;
break;
}
}
}
// Get an unordered list.
$item_list = [
'#theme' => 'item_list',
'#list_type' => 'ul',
'#items' => $items,
'#title' => '',
'#attributes' => [
'class' => [
'current-weather-summary',
],
],
];
// Get the weather icon.
$variables = [
'#theme' => 'wunderground_weather_current',
'#iconset' => $icon_set,
'#image' => [
'#theme' => 'image',
'#uri' => $this->wundergroundWeatherManager
->getIconUrl($config['icon_set'], $weather->current_observation->icon),
'#alt' => t('Weather in @city', [
'@city' => $weather->current_observation->display_location->full,
]),
],
'#summary' => $item_list,
];
$output = render($variables);
}
else {
// Return message if no data is retrieved.
$output = t('No weather forecast available.');
}
return [
'#children' => $output,
];
}