diff --git a/core/modules/comment/comment.admin.inc b/core/modules/comment/comment.admin.inc index 4360918e332..4bbff560ec2 100644 --- a/core/modules/comment/comment.admin.inc +++ b/core/modules/comment/comment.admin.inc @@ -5,9 +5,7 @@ * Admin page callbacks for the Comment module. */ -use Drupal\comment\Entity\Comment; -use Symfony\Component\HttpFoundation\RedirectResponse; -use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Drupal\comment\Form\ConfirmDeleteMultiple; /** * Page callback: Presents an administrative comment listing. @@ -20,10 +18,11 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; * @see comment_multiple_delete_confirm() */ function comment_admin($type = 'new') { - $edit = Drupal::request()->request->all(); + $request = Drupal::request(); + $edit = $request->request->all(); if (isset($edit['operation']) && ($edit['operation'] == 'delete') && isset($edit['comments']) && $edit['comments']) { - return drupal_get_form('comment_multiple_delete_confirm'); + return drupal_get_form(ConfirmDeleteMultiple::create(Drupal::getContainer()), $request); } else { return drupal_get_form('comment_admin_overview', $type); @@ -213,109 +212,3 @@ function comment_admin_overview_submit($form, &$form_state) { $form_state['redirect'] = 'admin/content/comment'; cache_invalidate_tags(array('content' => TRUE)); } - -/** - * Form constructor for the confirmation form for bulk comment deletion. - * - * @ingroup forms - * @see comment_admin() - * @see comment_multiple_delete_confirm_submit() - */ -function comment_multiple_delete_confirm($form, &$form_state) { - $edit = $form_state['input']; - - $form['comments'] = array( - '#prefix' => '', - '#tree' => TRUE, - ); - // array_filter() returns only elements with actual values. - $comment_counter = 0; - foreach (array_filter($edit['comments']) as $cid => $value) { - $comment = comment_load($cid); - if (is_object($comment) && is_numeric($comment->id())) { - $subject = db_query('SELECT subject FROM {comment} WHERE cid = :cid', array(':cid' => $cid))->fetchField(); - $form['comments'][$cid] = array('#type' => 'hidden', '#value' => $cid, '#prefix' => '
  • ', '#suffix' => check_plain($subject) . '
  • '); - $comment_counter++; - } - } - $form['operation'] = array('#type' => 'hidden', '#value' => 'delete'); - - if (!$comment_counter) { - drupal_set_message(t('There do not appear to be any comments to delete, or your selected comment was deleted by another administrator.')); - return new RedirectResponse(url('admin/content/comment', array('absolute' => TRUE))); - } - else { - return confirm_form($form, - t('Are you sure you want to delete these comments and all their children?'), - 'admin/content/comment', t('This action cannot be undone.'), - t('Delete comments'), t('Cancel')); - } -} - -/** - * Form submission handler for comment_multiple_delete_confirm(). - */ -function comment_multiple_delete_confirm_submit($form, &$form_state) { - if ($form_state['values']['confirm']) { - entity_delete_multiple('comment', array_keys($form_state['values']['comments'])); - cache_invalidate_tags(array('content' => TRUE)); - $count = count($form_state['values']['comments']); - watchdog('content', 'Deleted @count comments.', array('@count' => $count)); - drupal_set_message(format_plural($count, 'Deleted 1 comment.', 'Deleted @count comments.')); - } - $form_state['redirect'] = 'admin/content/comment'; -} - -/** - * Page callback: Shows a confirmation page for comment deletions. - * - * @param \Drupal\comment\Entity\Comment $comment - * The comment entity that is about to be deleted. - * - * @see comment_menu() - * @see comment_confirm_delete() - */ -function comment_confirm_delete_page(Comment $comment) { - return drupal_get_form('comment_confirm_delete', $comment); -} - -/** - * Form constructor for the confirmation form for comment deletion. - * - * @param Drupal\comment\Comment $comment - * The comment that is about to be deleted. - * - * @ingroup forms - * @see comment_confirm_delete_page() - * @see comment_confirm_delete_submit() - * @see confirm_form() - */ -function comment_confirm_delete($form, &$form_state, Comment $comment) { - $form_state['comment'] = $comment; - // Always provide entity id in the same form key as in the entity edit form. - $form['cid'] = array('#type' => 'value', '#value' => $comment->id()); - return confirm_form( - $form, - t('Are you sure you want to delete the comment %title?', array('%title' => $comment->subject->value)), - 'node/' . $comment->nid->target_id, - t('Any replies to this comment will be lost. This action cannot be undone.'), - t('Delete'), - t('Cancel'), - 'comment_confirm_delete'); -} - -/** - * Form submission handler for comment_confirm_delete(). - */ -function comment_confirm_delete_submit($form, &$form_state) { - $comment = $form_state['comment']; - // Delete the comment and its replies. - $comment->delete(); - drupal_set_message(t('The comment and all its replies have been deleted.')); - watchdog('content', 'Deleted comment @cid and its replies.', array('@cid' => $comment->id())); - // Clear the cache so an anonymous user sees that his comment was deleted. - cache_invalidate_tags(array('content' => TRUE)); - - $form_state['redirect'] = "node/{$comment->nid->target_id}"; -} diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index ef12cc11bee..921abbac422 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -224,12 +224,8 @@ function comment_menu() { ); $items['comment/%comment/delete'] = array( 'title' => 'Delete', - 'page callback' => 'comment_confirm_delete_page', - 'page arguments' => array(1), - 'access callback' => 'entity_page_access', - 'access arguments' => array(1, 'delete'), 'type' => MENU_LOCAL_TASK, - 'file' => 'comment.admin.inc', + 'route_name' => 'comment_confirm_delete', 'weight' => 20, ); $items['comment/reply/%node'] = array( diff --git a/core/modules/comment/comment.routing.yml b/core/modules/comment/comment.routing.yml index b62c118327f..7768cd07f34 100644 --- a/core/modules/comment/comment.routing.yml +++ b/core/modules/comment/comment.routing.yml @@ -19,3 +19,9 @@ comment_permalink: _controller: '\Drupal\comment\Controller\CommentController::commentPermalink' requirements: _entity_access: 'comment.view' +comment_confirm_delete: + pattern: '/comment/{comment}/delete' + defaults: + _entity_form: 'comment.delete' + requirements: + _entity_access: 'comment.delete' diff --git a/core/modules/comment/lib/Drupal/comment/Entity/Comment.php b/core/modules/comment/lib/Drupal/comment/Entity/Comment.php index 0dcf37d5eb5..007a97cae38 100644 --- a/core/modules/comment/lib/Drupal/comment/Entity/Comment.php +++ b/core/modules/comment/lib/Drupal/comment/Entity/Comment.php @@ -27,7 +27,8 @@ use Drupal\Core\Language\Language; * "access" = "Drupal\comment\CommentAccessController", * "render" = "Drupal\comment\CommentRenderController", * "form" = { - * "default" = "Drupal\comment\CommentFormController" + * "default" = "Drupal\comment\CommentFormController", + * "delete" = "Drupal\comment\Form\DeleteForm" * }, * "translation" = "Drupal\comment\CommentTranslationController" * }, diff --git a/core/modules/comment/lib/Drupal/comment/Form/ConfirmDeleteMultiple.php b/core/modules/comment/lib/Drupal/comment/Form/ConfirmDeleteMultiple.php new file mode 100644 index 00000000000..76be515e9df --- /dev/null +++ b/core/modules/comment/lib/Drupal/comment/Form/ConfirmDeleteMultiple.php @@ -0,0 +1,132 @@ +commentStorage = $comment_storage; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('plugin.manager.entity')->getStorageController('comment') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormID() { + return 'comment_multiple_delete_confirm'; + } + + /** + * {@inheritdoc} + */ + public function getQuestion() { + return $this->t('Are you sure you want to delete these comments and all their children?'); + } + + /** + * {@inheritdoc} + */ + public function getCancelPath() { + return 'admin/content/comment'; + } + + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return $this->t('Delete comments'); + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, array &$form_state, Request $request = NULL) { + $edit = $form_state['input']; + + $form['comments'] = array( + '#prefix' => '', + '#tree' => TRUE, + ); + // 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] = array( + '#type' => 'hidden', + '#value' => $cid, + '#prefix' => '
  • ', + '#suffix' => String::checkPlain($comment->label()) . '
  • ' + ); + $comment_counter++; + } + $form['operation'] = array('#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['redirect'] = 'admin/content/comment'; + } + + return parent::buildForm($form, $form_state, $request); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + if ($form_state['values']['confirm']) { + $this->commentStorage->delete($this->comments); + Cache::invalidateTags(array('content' => TRUE)); + $count = count($form_state['values']['comments']); + watchdog('content', 'Deleted @count comments.', array('@count' => $count)); + drupal_set_message(format_plural($count, 'Deleted 1 comment.', 'Deleted @count comments.')); + } + $form_state['redirect'] = 'admin/content/comment'; + } + +} diff --git a/core/modules/comment/lib/Drupal/comment/Form/DeleteForm.php b/core/modules/comment/lib/Drupal/comment/Form/DeleteForm.php new file mode 100644 index 00000000000..fd86ddfb7be --- /dev/null +++ b/core/modules/comment/lib/Drupal/comment/Form/DeleteForm.php @@ -0,0 +1,61 @@ +t('Are you sure you want to delete the comment %title?', array('%title' => $this->entity->subject->value)); + } + + /** + * {@inheritdoc} + */ + public function getCancelPath() { + return 'node/' . $this->entity->nid->target_id; + } + + /** + * {@inheritdoc} + */ + public function getDescription() { + return $this->t('Any replies to this comment will be lost. This action cannot be undone.'); + } + + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return $this->t('Delete'); + } + + /** + * {@inheritdoc} + */ + public function submit(array $form, array &$form_state) { + // Delete the comment and its replies. + $this->entity->delete(); + drupal_set_message($this->t('The comment and all its replies have been deleted.')); + watchdog('content', 'Deleted comment @cid and its replies.', array('@cid' => $this->entity->id())); + // Clear the cache so an anonymous user sees that his comment was deleted. + Cache::invalidateTags(array('content' => TRUE)); + + $form_state['redirect'] = "node/{$this->entity->nid->target_id}"; + } + +}