comparison core/modules/comment/src/Form/ConfirmDeleteMultiple.php @ 4:a9cd425dd02b

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