Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / redirect / src / Form / RedirectDeleteMultipleForm.php
1 <?php
2
3 namespace Drupal\redirect\Form;
4
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Form\ConfirmFormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Url;
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;
14
15 /**
16  * Provides a redirect deletion confirmation form.
17  */
18 class RedirectDeleteMultipleForm extends ConfirmFormBase {
19
20   /**
21    * The array of redirects to delete.
22    *
23    * @var string[][]
24    */
25   protected $redirects = [];
26
27   /**
28    * The private tempstore factory.
29    *
30    * @var \Drupal\user\PrivateTempStoreFactory
31    */
32   protected $privateTempStoreFactory;
33
34   /**
35    * The redirect storage.
36    *
37    * @var \Drupal\Core\Entity\EntityStorageInterface
38    */
39   protected $redirectStorage;
40
41   /**
42    * The current user.
43    *
44    * @var \Drupal\Core\Session\AccountInterface
45    */
46   protected $currentUser;
47
48   /**
49    * Constructs a RedirectDeleteMultiple form object.
50    *
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
56    *   The current user.
57    * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
58    *   The String translation.
59    */
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);
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public static function create(ContainerInterface $container) {
71     return new static(
72       $container->get('user.private_tempstore'),
73       $container->get('entity_type.manager'),
74       $container->get('current_user'),
75       $container->get('string_translation')
76     );
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public function getFormId() {
83     return 'redirect_multiple_delete_confirm';
84   }
85
86   /**
87    * {@inheritdoc}
88    */
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?');
91   }
92
93   /**
94    * {@inheritdoc}
95    */
96   public function getCancelUrl() {
97     return new Url('redirect.list');
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function getConfirmText() {
104     return $this->t('Delete');
105   }
106
107   /**
108    * {@inheritdoc}
109    */
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());
114     }
115
116     $form['redirects'] = [
117       '#theme' => 'item_list',
118       '#items' => array_map(function ($redirect) {
119         return $redirect->label();
120       }, $this->redirects),
121     ];
122     return parent::buildForm($form, $form_state);
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   public function submitForm(array &$form, FormStateInterface $form_state) {
129
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.'));
136     }
137     $form_state->setRedirect('redirect.list');
138   }
139
140 }