/** * {@inheritdoc} */ public function build() { // Get block configuration. $config = $this->getConfiguration(); $location = $config['location_forecast']; $number_of_days = $config['number_of_days']; $icon_set = $config['icon_set']; // Get all settings. $settings = \Drupal::config('wunderground_weather.settings'); preg_match('#\\[(.*?)\\]#', $location, $match); $path = $match[1]; $options = ['api' => 'api', 'key' => $settings->get('api_key'), 'data_feature' => 'forecast10day', 'language' => 'lang:' . strtoupper($settings->get('language')), 'path' => $path]; $request = new RequestDataController(); $data = $request->get($options); $days = $data->forecast->simpleforecast->forecastday; $variables['#theme'] = 'wunderground_weather_forecast'; $variables['#icon_set'] = $icon_set; $variables['#data'] = array_slice($days, 0, $number_of_days); $variables['#fields'] = $config['forecast_fields']; // Check if data is received. if ($data) { $output = render($variables); } else { // Return message if no data is retrieved. $output = t('No weather forecast available.'); } return ['#children' => $output]; }
/** * {@inheritdoc} */ public function build() { // Get block configuration. $config = $this->getConfiguration(); $location = $config['location_current']; $icon_set = $config['icon_set']; // Get all settings. $settings = \Drupal::config('wunderground_weather.settings'); 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]; $request = new RequestDataController(); $weather = $request->get($options); // Check if data is received. if ($weather) { // Calculate windspeed. $wind_kph = $weather->current_observation->wind_kph; $windspeed = _wunderground_weather_speed_to_beaufort($wind_kph, 'kph'); // Get fields to be displayed. $fields = $this->configuration['current_fields']; // Build list items. $items = []; foreach ($fields as $field => $display) { if ($display) { switch ($field) { case 'weather': $items[$field] = $weather->current_observation->weather; break; case 'temperature': $items[$field] = t('Temperature: !temp °C', ['!temp' => $weather->current_observation->temp_c]); break; case 'feels_like': $items[$field] = t('Feels like: !temp °C', ['!temp' => $weather->current_observation->feelslike_c]); break; case 'wind': $items[$field] = t('Wind') . ': ' . $windspeed . ' bft'; break; } } } // Get an unorderd list. $item_list = ['#theme' => 'item_list', '#list_type' => 'ul', '#items' => $items, '#title' => '', '#attributes' => ['class' => ['current-weather-summary']]]; $summary = render($item_list); // Get the weather icon. $variables = ['#theme' => 'wunderground_weather_current', '#iconset' => $icon_set, '#image' => ['#theme' => 'image', '#uri' => $this->getIconUrl($config['icon_set'], $weather->current_observation->icon), '#alt' => t('Weather in !city', ['!city' => $weather->current_observation->display_location->full]), 'title' => t('Weather in !city', ['!city' => $weather->current_observation->display_location->full])], '#summary' => $summary]; $output = render($variables); } else { // Return message if no data is retrieved. $output = t('No weather forecast available.'); } return ['#children' => $output]; }
/** * {@inheritdoc} */ public function blockSubmit($form, FormStateInterface $form_state) { $settings = \Drupal::config('wunderground_weather.settings'); $config = $this->getConfiguration(); preg_match('#\\[(.*?)\\]#', $config['location'], $match); $options = ['api' => 'api', 'key' => $settings->get('api_key'), 'data_feature' => 'geolookup', 'path' => $match[1]]; $request = new RequestDataController(); $geolookup = $request->get($options); $image_path = 'api/' . $settings->get('api_key') . '/animatedsatellite/image.gif'; $image_url = Url::fromUri(WUNDERGROUND_WEATHER_BASE_URL . $image_path, ['query' => ['borders' => 1, 'key' => 'sat_ir4', 'lat' => $geolookup->location->lat, 'lon' => $geolookup->location->lon, 'num' => 8, 'radius' => 200]]); $this->setConfigurationValue('location', $form_state->getValue(['location'])); $this->setConfigurationValue('image_url', $image_url->toUriString()); }