Newer
Older
Angie Byron
committed
<?php
namespace Drupal\comment\Form;
catch
committed
use Drupal\comment\CommentStorageInterface;
use Drupal\user\PrivateTempStoreFactory;
Angie Byron
committed
use Drupal\Core\Form\ConfirmFormBase;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
Angie Byron
committed
use Drupal\Core\Url;
Angie Byron
committed
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides the comment multiple delete confirmation form.
*/
Angie Byron
committed
class ConfirmDeleteMultiple extends ConfirmFormBase {
Angie Byron
committed
/**
* The tempstore factory.
*
* @var \Drupal\user\PrivateTempStoreFactory
*/
protected $tempStoreFactory;
Angie Byron
committed
/**
* The comment storage.
*
catch
committed
* @var \Drupal\comment\CommentStorageInterface
Angie Byron
committed
*/
protected $commentStorage;
/**
* An array of comments to be deleted.
*
* @var string[][]
Angie Byron
committed
*/
protected $commentInfo;
Angie Byron
committed
/**
* Creates an new ConfirmDeleteMultiple form.
*
catch
committed
* @param \Drupal\comment\CommentStorageInterface $comment_storage
Angie Byron
committed
* The comment storage.
* @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
* The tempstore factory.
Angie Byron
committed
*/
public function __construct(CommentStorageInterface $comment_storage, PrivateTempStoreFactory $temp_store_factory) {
Angie Byron
committed
$this->commentStorage = $comment_storage;
$this->tempStoreFactory = $temp_store_factory;
Angie Byron
committed
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager')->getStorage('comment'),
$container->get('user.private_tempstore')
Angie Byron
committed
);
}
/**
* {@inheritdoc}
*/
Alex Pott
committed
public function getFormId() {
Angie Byron
committed
return 'comment_multiple_delete_confirm';
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
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?');
Angie Byron
committed
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
Angie Byron
committed
return new Url('comment.admin');
Angie Byron
committed
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
Angie Byron
committed
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function buildForm(array $form, FormStateInterface $form_state) {
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
$this->commentInfo = $this->tempStoreFactory->get('comment_multiple_delete_confirm')->get($this->currentUser()->id());
if (empty($this->commentInfo)) {
return $this->redirect('comment.admin');
}
/** @var \Drupal\comment\CommentInterface[] $comments */
$comments = $this->commentStorage->loadMultiple(array_keys($this->commentInfo));
$items = [];
foreach ($this->commentInfo as $id => $langcodes) {
foreach ($langcodes as $langcode) {
$comment = $comments[$id]->getTranslation($langcode);
$key = $id . ':' . $langcode;
$default_key = $id . ':' . $comment->getUntranslated()->language()->getId();
// If we have a translated entity we build a nested list of translations
// that will be deleted.
$languages = $comment->getTranslationLanguages();
if (count($languages) > 1 && $comment->isDefaultTranslation()) {
$names = [];
foreach ($languages as $translation_langcode => $language) {
$names[] = $language->getName();
unset($items[$id . ':' . $translation_langcode]);
}
$items[$default_key] = [
'label' => [
'#markup' => $this->t('@label (Original translation) - <em>The following comment translations will be deleted:</em>', ['@label' => $comment->label()]),
],
'deleted_translations' => [
'#theme' => 'item_list',
'#items' => $names,
],
];
}
elseif (!isset($items[$default_key])) {
$items[$key] = $comment->label();
}
}
}
Angie Byron
committed
$form['comments'] = [
'#theme' => 'item_list',
'#items' => $items,
];
Angie Byron
committed
Angie Byron
committed
return parent::buildForm($form, $form_state);
Angie Byron
committed
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function submitForm(array &$form, FormStateInterface $form_state) {
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
if ($form_state->getValue('confirm') && !empty($this->commentInfo)) {
$total_count = 0;
$delete_comments = [];
/** @var \Drupal\Core\Entity\ContentEntityInterface[][] $delete_translations */
$delete_translations = [];
/** @var \Drupal\comment\CommentInterface[] $comments */
$comments = $this->commentStorage->loadMultiple(array_keys($this->commentInfo));
foreach ($this->commentInfo as $id => $langcodes) {
foreach ($langcodes as $langcode) {
$comment = $comments[$id]->getTranslation($langcode);
if ($comment->isDefaultTranslation()) {
$delete_comments[$id] = $comment;
unset($delete_translations[$id]);
$total_count += count($comment->getTranslationLanguages());
}
elseif (!isset($delete_comments[$id])) {
$delete_translations[$id][] = $comment;
}
}
}
if ($delete_comments) {
$this->commentStorage->delete($delete_comments);
$this->logger('content')->notice('Deleted @count comments.', ['@count' => count($delete_comments)]);
}
if ($delete_translations) {
$count = 0;
foreach ($delete_translations as $id => $translations) {
$comment = $comments[$id]->getUntranslated();
foreach ($translations as $translation) {
$comment->removeTranslation($translation->language()->getId());
}
$comment->save();
$count += count($translations);
}
if ($count) {
$total_count += $count;
$this->logger('content')->notice('Deleted @count comment translations.', ['@count' => $count]);
}
}
if ($total_count) {
drupal_set_message($this->formatPlural($total_count, 'Deleted 1 comment.', 'Deleted @count comments.'));
}
$this->tempStoreFactory->get('comment_multiple_delete_confirm')->delete($this->currentUser()->id());
Angie Byron
committed
}
Alex Pott
committed
$form_state->setRedirectUrl($this->getCancelUrl());