View source
<?php
namespace Drupal\weather\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\weather\Entity\WeatherPlaceInterface;
use Drupal\weather\Service\DataService;
use Drupal\weather\Service\HelperService;
use Drupal\weather\Service\ParserService;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AddCustomPlaceForm extends FormBase {
protected $entityTypeManager;
protected $weatherDisplayStorage;
protected $weatherDisplayPlaceStorage;
protected $weatherDataService;
protected $weatherHelper;
protected $weatherParser;
public function __construct(EntityTypeManagerInterface $entityTypeManager, DataService $weatherDataService, HelperService $helperService, ParserService $parserService, MessengerInterface $messenger) {
$this->entityTypeManager = $entityTypeManager;
$this->weatherDisplayStorage = $entityTypeManager
->getStorage('weather_display');
$this->weatherDisplayPlaceStorage = $entityTypeManager
->getStorage('weather_display_place');
$this->weatherDataService = $weatherDataService;
$this->weatherHelper = $helperService;
$this->weatherParser = $parserService;
$this->messenger = $messenger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('weather.data_service'), $container
->get('weather.helper'), $container
->get('weather.parser'), $container
->get('messenger'));
}
public function getFormId() {
return 'weather_settings_places';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$placesStorage = $this->entityTypeManager
->getStorage('weather_place');
$form['weather_yrno_url'] = [
'#type' => 'textfield',
'#title' => $this
->t('URL of English weather forecast on yr.no'),
'#description' => $this
->t('Example: https://www.yr.no/place/Ukraine/Kiev/Kyiv/.'),
'#required' => TRUE,
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save place'),
];
$tables = [
WeatherPlaceInterface::STATUS_ADDED => t('Added places'),
WeatherPlaceInterface::STATUS_MODIFIED => t('Modified places'),
];
foreach ($tables as $status => $caption) {
$header = [
$this
->t('GeoID'),
$this
->t('Latitude'),
$this
->t('Longitude'),
$this
->t('Country'),
$this
->t('Name'),
$this
->t('Link'),
];
$rows = [];
$result = $placesStorage
->getQuery()
->condition('status', $status)
->sort('country', 'ASC')
->sort('name', 'ASC')
->execute();
if (!empty($result)) {
foreach ($placesStorage
->loadMultiple($result) as $place) {
$rows[] = [
$place->geoid->value,
$place->latitude->value,
$place->longitude->value,
$place->country->value,
$place->name->value,
$place->link->value,
];
}
$form['places'][] = [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#caption' => $caption,
'#empty' => $this
->t('No places.'),
];
}
}
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$url = trim($form_state
->getValue('weather_yrno_url'));
if (substr($url, 0, 24) != 'https://www.yr.no/place/') {
$form_state
->setErrorByName('weather_yrno_url', $this
->t('Please make sure to use the English version of the forecast, starting with "https://www.yr.no/<strong>place</strong>/".'));
}
list($country, $link) = $this->weatherHelper
->parsePlaceUrl($url);
$placeExists = $this->entityTypeManager
->getStorage('weather_place')
->getQuery()
->condition('country', $country)
->condition('link', $link)
->execute();
if ($placeExists) {
$form_state
->setErrorByName('weather_yrno_url', $this
->t('The place is already in the database'));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$url = $form_state
->getValue('weather_yrno_url');
$url = trim($url) . 'forecast.xml';
if ($this->weatherParser
->downloadForecast('', $url)) {
$this->messenger
->addStatus($this
->t('The new place has been saved.'));
}
else {
$this->messenger
->addError($this
->t('The download from the given URL did not succeed.'));
}
}
}