LiveWeatherDeleteForm.php in Live Weather 8.2
File
src/Form/LiveWeatherDeleteForm.php
View source
<?php
namespace Drupal\live_weather\Form;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class LiveWeatherDeleteForm extends ConfirmFormBase {
private $woeid = NULL;
protected $configFactory;
public function __construct(ConfigFactoryInterface $config_factory) {
$this->configFactory = $config_factory;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'));
}
public function getFormId() {
return 'live_weather_delete_form';
}
public function getQuestion() {
return $this
->t('Delete location');
}
public function getDescription() {
return $this
->t('Are you sure you want to delete location?');
}
public function getCancelUrl() {
return new Url('live_weather.location');
}
public function buildForm(array $form, FormStateInterface $form_state, $woeid = NULL) {
$this->woeid = $woeid;
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$messenger = $this
->messenger();
$locations = $this->configFactory
->get('live_weather.location')
->get('location');
$woeid = $this->woeid;
if (array_key_exists($woeid, $locations)) {
unset($locations[$woeid]);
$this->configFactory
->getEditable('live_weather.location')
->set('location', $locations)
->save();
$form_state
->setRedirect('live_weather.location');
$msg = $this
->t('Your @woeid - Where On Earth IDentifier of location has been removed.', [
'@woeid' => $woeid,
]);
$messenger
->addMessage($msg);
}
else {
$msg = $this
->t('Your @woeid - Where On Earth IDentifier of location is not valid.', [
'@woeid' => $woeid,
]);
$messenger
->addWarning($msg);
}
}
}