WeatherDetailedForecastController.php in Weather 2.0.x
File
src/Controller/WeatherDetailedForecastController.php
View source
<?php
namespace Drupal\weather\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\weather\Entity\WeatherPlaceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class WeatherDetailedForecastController extends ControllerBase {
protected $entityTypeManager;
protected $displayPlaceStorage;
protected $weatherPlaceStorage;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->displayPlaceStorage = $this->entityTypeManager
->getStorage('weather_display_place');
$this->weatherPlaceStorage = $this->entityTypeManager
->getStorage('weather_place');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
public function detailedForecast(string $country, string $place, string $city, string $destination) {
$link = $place . '/' . $city;
$weatherPlace = $this->weatherPlaceStorage
->getQuery()
->condition('country', $country, 'LIKE')
->condition('link', $link, 'LIKE')
->execute();
if ($weatherPlace) {
$weatherPlace = $this->weatherPlaceStorage
->load(reset($weatherPlace));
}
$display_type = 'default';
$display_number = 0;
if (preg_match('/^[0-9]+$/', $destination)) {
$display_type = 'system-wide';
$display_number = $destination;
}
elseif ($destination == 'u') {
$display_type = 'user';
$display_number = $this
->currentUser()
->id();
}
if ($weatherPlace instanceof WeatherPlaceInterface) {
$configured = $this->displayPlaceStorage
->getQuery()
->condition('geoid', $weatherPlace
->id())
->condition('display_type', $display_type)
->condition('display_number', $display_number)
->execute();
if ($configured) {
return [
'#theme' => 'weather_detailed_forecast',
'#weather_display_place' => $this->displayPlaceStorage
->load(reset($configured)),
'#display_type' => $display_type,
'#display_number' => $display_number,
];
}
}
throw new NotFoundHttpException();
}
}