Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\comment\Form;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\comment\CommentStorageInterface;
|
Chris@0
|
6 use Drupal\user\PrivateTempStoreFactory;
|
Chris@0
|
7 use Drupal\Core\Form\ConfirmFormBase;
|
Chris@0
|
8 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
9 use Drupal\Core\Url;
|
Chris@0
|
10 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Provides the comment multiple delete confirmation form.
|
Chris@0
|
14 */
|
Chris@0
|
15 class ConfirmDeleteMultiple extends ConfirmFormBase {
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * The tempstore factory.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @var \Drupal\user\PrivateTempStoreFactory
|
Chris@0
|
21 */
|
Chris@0
|
22 protected $tempStoreFactory;
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * The comment storage.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @var \Drupal\comment\CommentStorageInterface
|
Chris@0
|
28 */
|
Chris@0
|
29 protected $commentStorage;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * An array of comments to be deleted.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @var string[][]
|
Chris@0
|
35 */
|
Chris@0
|
36 protected $commentInfo;
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * Creates an new ConfirmDeleteMultiple form.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @param \Drupal\comment\CommentStorageInterface $comment_storage
|
Chris@0
|
42 * The comment storage.
|
Chris@0
|
43 * @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
|
Chris@0
|
44 * The tempstore factory.
|
Chris@0
|
45 */
|
Chris@0
|
46 public function __construct(CommentStorageInterface $comment_storage, PrivateTempStoreFactory $temp_store_factory) {
|
Chris@0
|
47 $this->commentStorage = $comment_storage;
|
Chris@0
|
48 $this->tempStoreFactory = $temp_store_factory;
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 /**
|
Chris@0
|
52 * {@inheritdoc}
|
Chris@0
|
53 */
|
Chris@0
|
54 public static function create(ContainerInterface $container) {
|
Chris@0
|
55 return new static(
|
Chris@0
|
56 $container->get('entity.manager')->getStorage('comment'),
|
Chris@0
|
57 $container->get('user.private_tempstore')
|
Chris@0
|
58 );
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * {@inheritdoc}
|
Chris@0
|
63 */
|
Chris@0
|
64 public function getFormId() {
|
Chris@0
|
65 return 'comment_multiple_delete_confirm';
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * {@inheritdoc}
|
Chris@0
|
70 */
|
Chris@0
|
71 public function getQuestion() {
|
Chris@0
|
72 return $this->formatPlural(count($this->commentInfo), 'Are you sure you want to delete this comment and all its children?', 'Are you sure you want to delete these comments and all their children?');
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 /**
|
Chris@0
|
76 * {@inheritdoc}
|
Chris@0
|
77 */
|
Chris@0
|
78 public function getCancelUrl() {
|
Chris@0
|
79 return new Url('comment.admin');
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * {@inheritdoc}
|
Chris@0
|
84 */
|
Chris@0
|
85 public function getConfirmText() {
|
Chris@0
|
86 return $this->t('Delete');
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 /**
|
Chris@0
|
90 * {@inheritdoc}
|
Chris@0
|
91 */
|
Chris@0
|
92 public function buildForm(array $form, FormStateInterface $form_state) {
|
Chris@0
|
93 $this->commentInfo = $this->tempStoreFactory->get('comment_multiple_delete_confirm')->get($this->currentUser()->id());
|
Chris@0
|
94 if (empty($this->commentInfo)) {
|
Chris@0
|
95 return $this->redirect('comment.admin');
|
Chris@0
|
96 }
|
Chris@0
|
97 /** @var \Drupal\comment\CommentInterface[] $comments */
|
Chris@0
|
98 $comments = $this->commentStorage->loadMultiple(array_keys($this->commentInfo));
|
Chris@0
|
99
|
Chris@0
|
100 $items = [];
|
Chris@0
|
101 foreach ($this->commentInfo as $id => $langcodes) {
|
Chris@0
|
102 foreach ($langcodes as $langcode) {
|
Chris@0
|
103 $comment = $comments[$id]->getTranslation($langcode);
|
Chris@0
|
104 $key = $id . ':' . $langcode;
|
Chris@0
|
105 $default_key = $id . ':' . $comment->getUntranslated()->language()->getId();
|
Chris@0
|
106
|
Chris@0
|
107 // If we have a translated entity we build a nested list of translations
|
Chris@0
|
108 // that will be deleted.
|
Chris@0
|
109 $languages = $comment->getTranslationLanguages();
|
Chris@0
|
110 if (count($languages) > 1 && $comment->isDefaultTranslation()) {
|
Chris@0
|
111 $names = [];
|
Chris@0
|
112 foreach ($languages as $translation_langcode => $language) {
|
Chris@0
|
113 $names[] = $language->getName();
|
Chris@0
|
114 unset($items[$id . ':' . $translation_langcode]);
|
Chris@0
|
115 }
|
Chris@0
|
116 $items[$default_key] = [
|
Chris@0
|
117 'label' => [
|
Chris@0
|
118 '#markup' => $this->t('@label (Original translation) - <em>The following comment translations will be deleted:</em>', ['@label' => $comment->label()]),
|
Chris@0
|
119 ],
|
Chris@0
|
120 'deleted_translations' => [
|
Chris@0
|
121 '#theme' => 'item_list',
|
Chris@0
|
122 '#items' => $names,
|
Chris@0
|
123 ],
|
Chris@0
|
124 ];
|
Chris@0
|
125 }
|
Chris@0
|
126 elseif (!isset($items[$default_key])) {
|
Chris@0
|
127 $items[$key] = $comment->label();
|
Chris@0
|
128 }
|
Chris@0
|
129 }
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 $form['comments'] = [
|
Chris@0
|
133 '#theme' => 'item_list',
|
Chris@0
|
134 '#items' => $items,
|
Chris@0
|
135 ];
|
Chris@0
|
136
|
Chris@0
|
137 return parent::buildForm($form, $form_state);
|
Chris@0
|
138 }
|
Chris@0
|
139
|
Chris@0
|
140 /**
|
Chris@0
|
141 * {@inheritdoc}
|
Chris@0
|
142 */
|
Chris@0
|
143 public function submitForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
144 if ($form_state->getValue('confirm') && !empty($this->commentInfo)) {
|
Chris@0
|
145 $total_count = 0;
|
Chris@0
|
146 $delete_comments = [];
|
Chris@0
|
147 /** @var \Drupal\Core\Entity\ContentEntityInterface[][] $delete_translations */
|
Chris@0
|
148 $delete_translations = [];
|
Chris@0
|
149 /** @var \Drupal\comment\CommentInterface[] $comments */
|
Chris@0
|
150 $comments = $this->commentStorage->loadMultiple(array_keys($this->commentInfo));
|
Chris@0
|
151
|
Chris@0
|
152 foreach ($this->commentInfo as $id => $langcodes) {
|
Chris@0
|
153 foreach ($langcodes as $langcode) {
|
Chris@0
|
154 $comment = $comments[$id]->getTranslation($langcode);
|
Chris@0
|
155 if ($comment->isDefaultTranslation()) {
|
Chris@0
|
156 $delete_comments[$id] = $comment;
|
Chris@0
|
157 unset($delete_translations[$id]);
|
Chris@0
|
158 $total_count += count($comment->getTranslationLanguages());
|
Chris@0
|
159 }
|
Chris@0
|
160 elseif (!isset($delete_comments[$id])) {
|
Chris@0
|
161 $delete_translations[$id][] = $comment;
|
Chris@0
|
162 }
|
Chris@0
|
163 }
|
Chris@0
|
164 }
|
Chris@0
|
165
|
Chris@0
|
166 if ($delete_comments) {
|
Chris@0
|
167 $this->commentStorage->delete($delete_comments);
|
Chris@0
|
168 $this->logger('content')->notice('Deleted @count comments.', ['@count' => count($delete_comments)]);
|
Chris@0
|
169 }
|
Chris@0
|
170
|
Chris@0
|
171 if ($delete_translations) {
|
Chris@0
|
172 $count = 0;
|
Chris@0
|
173 foreach ($delete_translations as $id => $translations) {
|
Chris@0
|
174 $comment = $comments[$id]->getUntranslated();
|
Chris@0
|
175 foreach ($translations as $translation) {
|
Chris@0
|
176 $comment->removeTranslation($translation->language()->getId());
|
Chris@0
|
177 }
|
Chris@0
|
178 $comment->save();
|
Chris@0
|
179 $count += count($translations);
|
Chris@0
|
180 }
|
Chris@0
|
181 if ($count) {
|
Chris@0
|
182 $total_count += $count;
|
Chris@0
|
183 $this->logger('content')->notice('Deleted @count comment translations.', ['@count' => $count]);
|
Chris@0
|
184 }
|
Chris@0
|
185 }
|
Chris@0
|
186
|
Chris@0
|
187 if ($total_count) {
|
Chris@0
|
188 drupal_set_message($this->formatPlural($total_count, 'Deleted 1 comment.', 'Deleted @count comments.'));
|
Chris@0
|
189 }
|
Chris@0
|
190
|
Chris@0
|
191 $this->tempStoreFactory->get('comment_multiple_delete_confirm')->delete($this->currentUser()->id());
|
Chris@0
|
192 }
|
Chris@0
|
193
|
Chris@0
|
194 $form_state->setRedirectUrl($this->getCancelUrl());
|
Chris@0
|
195 }
|
Chris@0
|
196
|
Chris@0
|
197 }
|