WeatherDisplayBlockTrait.php in Weather 2.0.x
File
src/Plugin/Block/WeatherDisplayBlockTrait.php
View source
<?php
namespace Drupal\weather\Plugin\Block;
use Drupal\Core\Cache\Cache;
use Drupal\weather\Entity\WeatherDisplayInterface;
trait WeatherDisplayBlockTrait {
protected $weatherDisplay;
protected $weatherDisplayPlaceStorage;
protected $currentUser;
protected $destination;
public function build() {
if (!$this->weatherDisplay instanceof WeatherDisplayInterface) {
return [];
}
$type = $this->weatherDisplay->type->value;
$number = $this->weatherDisplay->number->value;
$display_places = $this->weatherDisplayPlaceStorage
->loadByProperties([
'display_type' => $type,
'display_number' => $number,
]);
if (empty($display_places)) {
return [];
}
$build = [
'#theme' => 'weather',
'#display_type' => $type,
'#display_number' => $number,
'#destination' => $this->destination,
];
return $build;
}
public function getCacheTags() {
$parent = parent::getCacheTags();
$displayTags = $this->weatherDisplay
->getCacheTags();
$tags = Cache::mergeTags($parent, $displayTags);
$display_places = $this->weatherDisplayPlaceStorage
->loadByProperties([
'display_type' => $this->weatherDisplay->type->value,
'display_number' => $this->weatherDisplay->number->value,
]);
foreach ($display_places as $display_place) {
$tags = Cache::mergeTags($tags, $display_place
->getCacheTags());
}
$tags = Cache::mergeTags($tags, [
'config:weather.settings',
]);
return $tags;
}
}