annotate core/modules/comment/src/Form/ConfirmDeleteMultiple.php @ 14:1fec387a4317

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