3 namespace Drupal\comment\Form;
5 use Drupal\comment\CommentStorageInterface;
6 use Drupal\Core\TempStore\PrivateTempStoreFactory;
7 use Drupal\Core\Form\ConfirmFormBase;
8 use Drupal\Core\Form\FormStateInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Provides the comment multiple delete confirmation form.
17 class ConfirmDeleteMultiple extends ConfirmFormBase {
20 * The tempstore factory.
22 * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
24 protected $tempStoreFactory;
27 * The comment storage.
29 * @var \Drupal\comment\CommentStorageInterface
31 protected $commentStorage;
34 * An array of comments to be deleted.
38 protected $commentInfo;
41 * Creates an new ConfirmDeleteMultiple form.
43 * @param \Drupal\comment\CommentStorageInterface $comment_storage
44 * The comment storage.
45 * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
46 * The tempstore factory.
48 public function __construct(CommentStorageInterface $comment_storage, PrivateTempStoreFactory $temp_store_factory) {
49 $this->commentStorage = $comment_storage;
50 $this->tempStoreFactory = $temp_store_factory;
56 public static function create(ContainerInterface $container) {
58 $container->get('entity.manager')->getStorage('comment'),
59 $container->get('tempstore.private')
66 public function getFormId() {
67 return 'comment_multiple_delete_confirm';
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?');
80 public function getCancelUrl() {
81 return new Url('comment.admin');
87 public function getConfirmText() {
88 return $this->t('Delete');
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');
99 /** @var \Drupal\comment\CommentInterface[] $comments */
100 $comments = $this->commentStorage->loadMultiple(array_keys($this->commentInfo));
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();
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()) {
114 foreach ($languages as $translation_langcode => $language) {
115 $names[] = $language->getName();
116 unset($items[$id . ':' . $translation_langcode]);
118 $items[$default_key] = [
120 '#markup' => $this->t('@label (Original translation) - <em>The following comment translations will be deleted:</em>', ['@label' => $comment->label()]),
122 'deleted_translations' => [
123 '#theme' => 'item_list',
128 elseif (!isset($items[$default_key])) {
129 $items[$key] = $comment->label();
134 $form['comments'] = [
135 '#theme' => 'item_list',
139 return parent::buildForm($form, $form_state);
145 public function submitForm(array &$form, FormStateInterface $form_state) {
146 if ($form_state->getValue('confirm') && !empty($this->commentInfo)) {
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));
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());
162 elseif (!isset($delete_comments[$id])) {
163 $delete_translations[$id][] = $comment;
168 if ($delete_comments) {
169 $this->commentStorage->delete($delete_comments);
170 $this->logger('content')->notice('Deleted @count comments.', ['@count' => count($delete_comments)]);
173 if ($delete_translations) {
175 foreach ($delete_translations as $id => $translations) {
176 $comment = $comments[$id]->getUntranslated();
177 foreach ($translations as $translation) {
178 $comment->removeTranslation($translation->language()->getId());
181 $count += count($translations);
184 $total_count += $count;
185 $this->logger('content')->notice('Deleted @count comment translations.', ['@count' => $count]);
190 drupal_set_message($this->formatPlural($total_count, 'Deleted 1 comment.', 'Deleted @count comments.'));
193 $this->tempStoreFactory->get('comment_multiple_delete_confirm')->delete($this->currentUser()->id());
196 $form_state->setRedirectUrl($this->getCancelUrl());