diff --git a/core/modules/comment/src/Form/ConfirmDeleteMultiple.php b/core/modules/comment/src/Form/ConfirmDeleteMultiple.php index 1f51090..09ff7de 100644 --- a/core/modules/comment/src/Form/ConfirmDeleteMultiple.php +++ b/core/modules/comment/src/Form/ConfirmDeleteMultiple.php @@ -37,9 +37,9 @@ 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. @@ -75,7 +75,7 @@ public function getFormId() { * {@inheritdoc} */ public function getQuestion() { - return $this->formatPlural(count($this->comments), '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?'); + 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?'); } /** @@ -96,28 +96,50 @@ public function getConfirmText() { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { - $this->comments = $this->tempStoreFactory->get('comment_multiple_delete_confirm')->get($this->currentUser()->id()); - if (empty($this->comments)) { + $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) - The following content translations will be deleted:', ['@label' => $node->label()]), + ], + 'deleted_translations' => [ + '#theme' => 'item_list', + '#items' => $names, + ], + ]; + } + elseif (!isset($items[$default_key])) { + $items[$key] = $comment->label(); + } + } + } $form['comments'] = array( - '#prefix' => '', - '#tree' => TRUE, + '#theme' => 'item_list', + '#items' => $items, ); - foreach ($this->comments as $comment) { - $cid = $comment->id(); - $form['comments'][$cid] = array( - '#type' => 'hidden', - '#value' => $cid, - '#prefix' => '
  • ', - '#suffix' => Html::escape($comment->label()) . '
  • ' - ); - } - $form['operation'] = array('#type' => 'hidden', '#value' => 'delete'); - return parent::buildForm($form, $form_state); } @@ -125,13 +147,56 @@ public function buildForm(array $form, FormStateInterface $form_state) { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - if ($form_state->getValue('confirm') && !empty($this->comments)) { - $this->commentStorage->delete($this->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.', array('@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.', array('@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()); - $count = count($this->comments); - $this->logger('content')->notice('Deleted @count comments.', array('@count' => $count)); - drupal_set_message($this->formatPlural($count, 'Deleted 1 comment.', 'Deleted @count comments.')); } + $form_state->setRedirectUrl($this->getCancelUrl()); } diff --git a/core/modules/comment/src/Plugin/Action/DeleteComment.php b/core/modules/comment/src/Plugin/Action/DeleteComment.php index ee759f3..db30f01 100644 --- a/core/modules/comment/src/Plugin/Action/DeleteComment.php +++ b/core/modules/comment/src/Plugin/Action/DeleteComment.php @@ -74,7 +74,13 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ public function executeMultiple(array $entities) { - $this->tempStore->set($this->currentUser->id(), $entities); + $info = []; + /** @var \Drupal\comment\CommentInterface $comment */ + foreach ($entities as $comment) { + $langcode = $comment->language()->getId(); + $info[$comment->id()][$langcode] = $langcode; + } + $this->tempStore->set($this->currentUser->id(), $info); } /** @@ -88,7 +94,7 @@ public function execute($entity = NULL) { * {@inheritdoc} */ public function access($comment, AccountInterface $account = NULL, $return_as_object = FALSE) { - /** @var \Drupal\comment\CommentInterface $object */ + /** @var \Drupal\comment\CommentInterface $comment */ return $comment->access('delete', $account, $return_as_object); } diff --git a/core/modules/comment/src/Plugin/views/field/CommentBulkForm.php b/core/modules/comment/src/Plugin/views/field/CommentBulkForm.php index 0e83fc2..64face3 100644 --- a/core/modules/comment/src/Plugin/views/field/CommentBulkForm.php +++ b/core/modules/comment/src/Plugin/views/field/CommentBulkForm.php @@ -20,22 +20,6 @@ class CommentBulkForm extends BulkForm { /** * {@inheritdoc} */ - protected function calculateEntityBulkFormKey(EntityInterface $entity, $use_revision) { - return $entity->id(); - } - - /** - * {@inheritdoc} - */ - protected function loadEntityFromBulkFormKey($id) { - return $this->entityManager - ->getStorage($this->getEntityType()) - ->load($id); - } - - /** - * {@inheritdoc} - */ protected function emptySelectedMessage() { return $this->t('Select one or more comments to perform the update on.'); }