Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / comment / src / Form / ConfirmDeleteMultiple.php
index 590624831632a47541b5d7c3f394bc90cf727533..1be7a68c157b843e2e07db8850c5bc3296000c93 100644 (file)
@@ -3,7 +3,7 @@
 namespace Drupal\comment\Form;
 
 use Drupal\comment\CommentStorageInterface;
-use Drupal\Component\Utility\Html;
+use Drupal\user\PrivateTempStoreFactory;
 use Drupal\Core\Form\ConfirmFormBase;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Url;
@@ -14,6 +14,13 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
  */
 class ConfirmDeleteMultiple extends ConfirmFormBase {
 
+  /**
+   * The tempstore factory.
+   *
+   * @var \Drupal\user\PrivateTempStoreFactory
+   */
+  protected $tempStoreFactory;
+
   /**
    * The comment storage.
    *
@@ -24,18 +31,21 @@ class ConfirmDeleteMultiple extends ConfirmFormBase {
   /**
    * An array of comments to be deleted.
    *
-   * @var \Drupal\comment\CommentInterface[]
+   * @var string[][]
    */
-  protected $comments;
+  protected $commentInfo;
 
   /**
    * Creates an new ConfirmDeleteMultiple form.
    *
    * @param \Drupal\comment\CommentStorageInterface $comment_storage
    *   The comment storage.
+   * @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
+   *   The tempstore factory.
    */
-  public function __construct(CommentStorageInterface $comment_storage) {
+  public function __construct(CommentStorageInterface $comment_storage, PrivateTempStoreFactory $temp_store_factory) {
     $this->commentStorage = $comment_storage;
+    $this->tempStoreFactory = $temp_store_factory;
   }
 
   /**
@@ -43,7 +53,8 @@ class ConfirmDeleteMultiple extends ConfirmFormBase {
    */
   public static function create(ContainerInterface $container) {
     return new static(
-      $container->get('entity.manager')->getStorage('comment')
+      $container->get('entity.manager')->getStorage('comment'),
+      $container->get('user.private_tempstore')
     );
   }
 
@@ -58,7 +69,7 @@ class ConfirmDeleteMultiple extends ConfirmFormBase {
    * {@inheritdoc}
    */
   public function getQuestion() {
-    return $this->t('Are you sure you want to delete these comments and all their children?');
+    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?');
   }
 
   /**
@@ -72,39 +83,56 @@ class ConfirmDeleteMultiple extends ConfirmFormBase {
    * {@inheritdoc}
    */
   public function getConfirmText() {
-    return $this->t('Delete comments');
+    return $this->t('Delete');
   }
 
   /**
    * {@inheritdoc}
    */
   public function buildForm(array $form, FormStateInterface $form_state) {
-    $edit = $form_state->getUserInput();
+    $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();
+        }
+      }
+    }
 
     $form['comments'] = [
-      '#prefix' => '<ul>',
-      '#suffix' => '</ul>',
-      '#tree' => TRUE,
+      '#theme' => 'item_list',
+      '#items' => $items,
     ];
-    // array_filter() returns only elements with actual values.
-    $comment_counter = 0;
-    $this->comments = $this->commentStorage->loadMultiple(array_keys(array_filter($edit['comments'])));
-    foreach ($this->comments as $comment) {
-      $cid = $comment->id();
-      $form['comments'][$cid] = [
-        '#type' => 'hidden',
-        '#value' => $cid,
-        '#prefix' => '<li>',
-        '#suffix' => Html::escape($comment->label()) . '</li>'
-      ];
-      $comment_counter++;
-    }
-    $form['operation'] = ['#type' => 'hidden', '#value' => 'delete'];
-
-    if (!$comment_counter) {
-      drupal_set_message($this->t('There do not appear to be any comments to delete, or your selected comment was deleted by another administrator.'));
-      $form_state->setRedirect('comment.admin');
-    }
 
     return parent::buildForm($form, $form_state);
   }
@@ -113,12 +141,56 @@ class ConfirmDeleteMultiple extends ConfirmFormBase {
    * {@inheritdoc}
    */
   public function submitForm(array &$form, FormStateInterface $form_state) {
-    if ($form_state->getValue('confirm')) {
-      $this->commentStorage->delete($this->comments);
-      $count = count($form_state->getValue('comments'));
-      $this->logger('comment')->notice('Deleted @count comments.', ['@count' => $count]);
-      drupal_set_message($this->formatPlural($count, 'Deleted 1 comment.', 'Deleted @count comments.'));
+    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());
     }
+
     $form_state->setRedirectUrl($this->getCancelUrl());
   }