class AddCustomPlaceForm in Weather 8
Same name and namespace in other branches
- 2.0.x src/Form/AddCustomPlaceForm.php \Drupal\weather\Form\AddCustomPlaceForm
Configure Weather settings for this site.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\weather\Form\AddCustomPlaceForm
Expanded class hierarchy of AddCustomPlaceForm
1 string reference to 'AddCustomPlaceForm'
File
- src/
Form/ AddCustomPlaceForm.php, line 19
Namespace
Drupal\weather\FormView source
class AddCustomPlaceForm extends FormBase {
/**
* Entity Type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Weather displays storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $weatherDisplayStorage;
/**
* Weather display places storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $weatherDisplayPlaceStorage;
/**
* Weather Data service.
*
* @var \Drupal\weather\Service\DataService
*/
protected $weatherDataService;
/**
* Weather helper service.
*
* @var \Drupal\weather\Service\HelperService
*/
protected $weatherHelper;
/**
* Parser service.
*
* @var \Drupal\weather\Service\ParserService
*/
protected $weatherParser;
/**
* Constructs a \Drupal\weather\Form\SettingsForm object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* Entity type manager storage.
* @param \Drupal\weather\Service\DataService $weatherDataService
* Weather data service.
* @param \Drupal\weather\Service\HelperService $helperService
* Weather helper service.
* @param \Drupal\weather\Service\ParserService $parserService
* Weather parser service.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* Drupal messenegr service.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
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;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'weather_settings_places';
}
/**
* {@inheritdoc}
*/
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'),
];
// Create tables for modified places.
$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;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// Remove whitespaces.
$url = trim($form_state
->getValue('weather_yrno_url'));
// Check for the english version.
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'));
}
}
/**
* {@inheritdoc}
*/
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.'));
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AddCustomPlaceForm:: |
protected | property | Entity Type manager service. | |
AddCustomPlaceForm:: |
protected | property | Weather Data service. | |
AddCustomPlaceForm:: |
protected | property | Weather display places storage. | |
AddCustomPlaceForm:: |
protected | property | Weather displays storage. | |
AddCustomPlaceForm:: |
protected | property | Weather helper service. | |
AddCustomPlaceForm:: |
protected | property | Parser service. | |
AddCustomPlaceForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
AddCustomPlaceForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
AddCustomPlaceForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
AddCustomPlaceForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
AddCustomPlaceForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
AddCustomPlaceForm:: |
public | function | Constructs a \Drupal\weather\Form\SettingsForm object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |