WeatherDisplayDeleteForm.php in Weather 8
File
src/Form/WeatherDisplayDeleteForm.php
View source
<?php
namespace Drupal\weather\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\weather\Entity\WeatherDisplayInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WeatherDisplayDeleteForm extends ContentEntityConfirmFormBase {
protected $block_manager;
public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, BlockManagerInterface $block_manager) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->block_manager = $block_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.repository'), $container
->get('entity_type.bundle.info'), $container
->get('datetime.time'), $container
->get('plugin.manager.block'));
}
public function getQuestion() {
return $this
->t('Are sure you want to remove this weather display?');
}
public function getDescription() {
return $this
->t('Related block and display places will be removed as well. This action cannot be undone.');
}
public function getCancelUrl() {
return Url::fromRoute('weather.settings');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$display_type = $this->entity->type->value;
$display_number = $this->entity->number->value;
$this->entity
->delete();
$this
->messenger()
->addStatus($this
->t('Weather display was removed.'));
switch ($display_type) {
case WeatherDisplayInterface::USER_TYPE:
$form_state
->setRedirectUrl(Url::fromRoute('weather.user.settings', [
'user' => $display_number,
]));
break;
default:
$form_state
->setRedirectUrl(Url::fromRoute('weather.settings'));
break;
}
$this->block_manager
->clearCachedDefinitions();
parent::submitForm($form, $form_state);
}
}