You are here

public function LiveWeatherBlock::build in Live Weather 8

Same name and namespace in other branches
  1. 8.2 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\Block

Code

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');
  array_push($location_list, '0');
  $location_value = implode(",", $location_list);
  $html = array();
  $feed = array();
  $feed_data = $this->liveWeather;
  if (!empty($location_list)) {
    $feed = $feed_data
      ->locationCheck($location_value, ' * ', $settings['unit']);
  }
  if (is_array($feed) && !empty($feed)) {
    $result = $feed['query']['results']['channel'];
    $i = 0;
    foreach ($result as $data) {
      if (!empty($data['location']['city'])) {
        $temp = Html::escape($data['item']['condition']['temp']);
        $date = Html::escape($data['item']['pubDate']);
        $feed_sunrise = Html::escape($data['astronomy']['sunrise']);
        $feed_sunset = Html::escape($data['astronomy']['sunset']);
        $daynight = $feed_data
          ->checkDayNight($date, $feed_sunrise, $feed_sunset);
        $wind_direction = $feed_data
          ->windDirection(Html::escape($data['wind']['direction']));
        $html[$i]['location'] = Html::escape($data['location']['city']) . ', ' . Html::escape($data['location']['region']) . ', ' . Html::escape($data['location']['country']);
        $html[$i]['temperature'] = $temp;
        $html[$i]['temperature_unit'] = $settings['unit'];
        $html[$i]['text'] = Html::escape($data['item']['condition']['text']);
        if ($settings['image']) {
          $html[$i]['image'] = 'http://l.yimg.com/a/i/us/nws/weather/gr/' . Html::escape($data['item']['condition']['code']) . $daynight;
        }
        if ($settings['wind']) {
          $html[$i]['wind'] = Html::escape($data['wind']['speed']) . ' mph ' . $wind_direction;
        }
        if ($settings['humidity']) {
          $html[$i]['humidity'] = Html::escape($data['atmosphere']['humidity']);
        }
        if ($settings['visibility']) {
          $html[$i]['visibility'] = Html::escape($data['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'],
    ),
  );
}