3 namespace Drupal\redirect\Form;
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Form\ConfirmFormBase;
7 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\user\PrivateTempStoreFactory;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11 use Symfony\Component\HttpFoundation\RedirectResponse;
12 use Drupal\Core\Session\AccountInterface;
13 use Drupal\Core\StringTranslation\TranslationInterface;
16 * Provides a redirect deletion confirmation form.
18 class RedirectDeleteMultipleForm extends ConfirmFormBase {
21 * The array of redirects to delete.
25 protected $redirects = [];
28 * The private tempstore factory.
30 * @var \Drupal\user\PrivateTempStoreFactory
32 protected $privateTempStoreFactory;
35 * The redirect storage.
37 * @var \Drupal\Core\Entity\EntityStorageInterface
39 protected $redirectStorage;
44 * @var \Drupal\Core\Session\AccountInterface
46 protected $currentUser;
49 * Constructs a RedirectDeleteMultiple form object.
51 * @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
52 * The tempstore factory.
53 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
54 * The entity type manager.
55 * @param \Drupal\Core\Session\AccountInterface $account
57 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
58 * The String translation.
60 public function __construct(PrivateTempStoreFactory $temp_store_factory, EntityTypeManagerInterface $entity_type_manager, AccountInterface $account, TranslationInterface $string_translation) {
61 $this->privateTempStoreFactory = $temp_store_factory;
62 $this->redirectStorage = $entity_type_manager->getStorage('redirect');
63 $this->currentUser = $account;
64 $this->setStringTranslation($string_translation);
70 public static function create(ContainerInterface $container) {
72 $container->get('user.private_tempstore'),
73 $container->get('entity_type.manager'),
74 $container->get('current_user'),
75 $container->get('string_translation')
82 public function getFormId() {
83 return 'redirect_multiple_delete_confirm';
89 public function getQuestion() {
90 return $this->formatPlural(count($this->redirects), 'Are you sure you want to delete this redirect?', 'Are you sure you want to delete these redirects?');
96 public function getCancelUrl() {
97 return new Url('redirect.list');
103 public function getConfirmText() {
110 public function buildForm(array $form, FormStateInterface $form_state) {
111 $this->redirects = $this->privateTempStoreFactory->get('redirect_multiple_delete_confirm')->get($this->currentUser->id());
112 if (empty($this->redirects)) {
113 return new RedirectResponse($this->getCancelUrl()->setAbsolute()->toString());
116 $form['redirects'] = [
117 '#theme' => 'item_list',
118 '#items' => array_map(function ($redirect) {
119 return $redirect->label();
120 }, $this->redirects),
122 return parent::buildForm($form, $form_state);
128 public function submitForm(array &$form, FormStateInterface $form_state) {
130 if ($form_state->getValue('confirm') && !empty($this->redirects)) {
131 $this->redirectStorage->delete($this->redirects);
132 $this->privateTempStoreFactory->get('redirect_multiple_delete_confirm')->delete($this->currentUser->id());
133 $count = count($this->redirects);
134 $this->logger('redirect')->notice('Deleted @count redirects.', ['@count' => $count]);
135 drupal_set_message($this->stringTranslation->formatPlural($count, 'Deleted 1 redirect.', 'Deleted @count redirects.'));
137 $form_state->setRedirect('redirect.list');