Issue #1946348 by splatio, jibran, kim.pepper, David Hernández, tim.plunkett | mtift: Convert all of confirm_form() in comment.admin.inc to the new form interface.

8.0.x
webchick 2013-08-30 23:30:35 -07:00
parent 8bd23e4bdd
commit d02d037e3e
6 changed files with 206 additions and 117 deletions

View File

@ -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' => '<ul>',
'#suffix' => '</ul>',
'#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' => '<li>', '#suffix' => check_plain($subject) . '</li>');
$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}";
}

View File

@ -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(

View File

@ -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'

View File

@ -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"
* },

View File

@ -0,0 +1,132 @@
<?php
/**
* @file
* Contains \Drupal\comment\Form\DeleteConfirmMultiple.
*/
namespace Drupal\comment\Form;
use Drupal\comment\CommentStorageControllerInterface;
use Drupal\Component\Utility\String;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Controller\ControllerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides the comment multiple delete confirmation form.
*/
class ConfirmDeleteMultiple extends ConfirmFormBase implements ControllerInterface {
/**
* The comment storage.
*
* @var \Drupal\comment\CommentStorageControllerInterface
*/
protected $commentStorage;
/**
* An array of comments to be deleted.
*
* @var \Drupal\comment\CommentInterface[]
*/
protected $comments;
/**
* Creates an new ConfirmDeleteMultiple form.
*
* @param \Drupal\comment\CommentStorageControllerInterface $comment_storage
* The comment storage.
*/
public function __construct(CommentStorageControllerInterface $comment_storage) {
$this->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' => '<ul>',
'#suffix' => '</ul>',
'#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' => '<li>',
'#suffix' => String::checkPlain($comment->label()) . '</li>'
);
$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';
}
}

View File

@ -0,0 +1,61 @@
<?php
/**
* @file
* Contains \Drupal\comment\Form\DeleteForm.
*/
namespace Drupal\comment\Form;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityNGConfirmFormBase;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides the comment delete confirmation form.
*/
class DeleteForm extends EntityNGConfirmFormBase {
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->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}";
}
}