Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / comment / src / Form / ConfirmDeleteMultiple.php
1 <?php
2
3 namespace Drupal\comment\Form;
4
5 use Drupal\comment\CommentStorageInterface;
6 use Drupal\Core\TempStore\PrivateTempStoreFactory;
7 use Drupal\Core\Form\ConfirmFormBase;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Url;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Provides the comment multiple delete confirmation form.
14  *
15  * @internal
16  */
17 class ConfirmDeleteMultiple extends ConfirmFormBase {
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
70   /**
71    * {@inheritdoc}
72    */
73   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?');
75   }
76
77   /**
78    * {@inheritdoc}
79    */
80   public function getCancelUrl() {
81     return new Url('comment.admin');
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function getConfirmText() {
88     return $this->t('Delete');
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function buildForm(array $form, FormStateInterface $form_state) {
95     $this->commentInfo = $this->tempStoreFactory->get('comment_multiple_delete_confirm')->get($this->currentUser()->id());
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   }
198
199 }