Chris@0: commentStorage = $comment_storage; Chris@0: $this->tempStoreFactory = $temp_store_factory; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function create(ContainerInterface $container) { Chris@0: return new static( Chris@0: $container->get('entity.manager')->getStorage('comment'), Chris@14: $container->get('tempstore.private') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getFormId() { Chris@0: return 'comment_multiple_delete_confirm'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getQuestion() { Chris@0: 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: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getCancelUrl() { Chris@0: return new Url('comment.admin'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getConfirmText() { Chris@0: return $this->t('Delete'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function buildForm(array $form, FormStateInterface $form_state) { Chris@0: $this->commentInfo = $this->tempStoreFactory->get('comment_multiple_delete_confirm')->get($this->currentUser()->id()); Chris@0: if (empty($this->commentInfo)) { Chris@0: return $this->redirect('comment.admin'); Chris@0: } Chris@0: /** @var \Drupal\comment\CommentInterface[] $comments */ Chris@0: $comments = $this->commentStorage->loadMultiple(array_keys($this->commentInfo)); Chris@0: Chris@0: $items = []; Chris@0: foreach ($this->commentInfo as $id => $langcodes) { Chris@0: foreach ($langcodes as $langcode) { Chris@0: $comment = $comments[$id]->getTranslation($langcode); Chris@0: $key = $id . ':' . $langcode; Chris@0: $default_key = $id . ':' . $comment->getUntranslated()->language()->getId(); Chris@0: Chris@0: // If we have a translated entity we build a nested list of translations Chris@0: // that will be deleted. Chris@0: $languages = $comment->getTranslationLanguages(); Chris@0: if (count($languages) > 1 && $comment->isDefaultTranslation()) { Chris@0: $names = []; Chris@0: foreach ($languages as $translation_langcode => $language) { Chris@0: $names[] = $language->getName(); Chris@0: unset($items[$id . ':' . $translation_langcode]); Chris@0: } Chris@0: $items[$default_key] = [ Chris@0: 'label' => [ Chris@0: '#markup' => $this->t('@label (Original translation) - The following comment translations will be deleted:', ['@label' => $comment->label()]), Chris@0: ], Chris@0: 'deleted_translations' => [ Chris@0: '#theme' => 'item_list', Chris@0: '#items' => $names, Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: elseif (!isset($items[$default_key])) { Chris@0: $items[$key] = $comment->label(); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: $form['comments'] = [ Chris@0: '#theme' => 'item_list', Chris@0: '#items' => $items, Chris@0: ]; Chris@0: Chris@0: return parent::buildForm($form, $form_state); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function submitForm(array &$form, FormStateInterface $form_state) { Chris@0: if ($form_state->getValue('confirm') && !empty($this->commentInfo)) { Chris@0: $total_count = 0; Chris@0: $delete_comments = []; Chris@0: /** @var \Drupal\Core\Entity\ContentEntityInterface[][] $delete_translations */ Chris@0: $delete_translations = []; Chris@0: /** @var \Drupal\comment\CommentInterface[] $comments */ Chris@0: $comments = $this->commentStorage->loadMultiple(array_keys($this->commentInfo)); Chris@0: Chris@0: foreach ($this->commentInfo as $id => $langcodes) { Chris@0: foreach ($langcodes as $langcode) { Chris@0: $comment = $comments[$id]->getTranslation($langcode); Chris@0: if ($comment->isDefaultTranslation()) { Chris@0: $delete_comments[$id] = $comment; Chris@0: unset($delete_translations[$id]); Chris@0: $total_count += count($comment->getTranslationLanguages()); Chris@0: } Chris@0: elseif (!isset($delete_comments[$id])) { Chris@0: $delete_translations[$id][] = $comment; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: if ($delete_comments) { Chris@0: $this->commentStorage->delete($delete_comments); Chris@0: $this->logger('content')->notice('Deleted @count comments.', ['@count' => count($delete_comments)]); Chris@0: } Chris@0: Chris@0: if ($delete_translations) { Chris@0: $count = 0; Chris@0: foreach ($delete_translations as $id => $translations) { Chris@0: $comment = $comments[$id]->getUntranslated(); Chris@0: foreach ($translations as $translation) { Chris@0: $comment->removeTranslation($translation->language()->getId()); Chris@0: } Chris@0: $comment->save(); Chris@0: $count += count($translations); Chris@0: } Chris@0: if ($count) { Chris@0: $total_count += $count; Chris@0: $this->logger('content')->notice('Deleted @count comment translations.', ['@count' => $count]); Chris@0: } Chris@0: } Chris@0: Chris@0: if ($total_count) { Chris@0: drupal_set_message($this->formatPlural($total_count, 'Deleted 1 comment.', 'Deleted @count comments.')); Chris@0: } Chris@0: Chris@0: $this->tempStoreFactory->get('comment_multiple_delete_confirm')->delete($this->currentUser()->id()); Chris@0: } Chris@0: Chris@0: $form_state->setRedirectUrl($this->getCancelUrl()); Chris@0: } Chris@0: Chris@0: }