Newer
Older
Angie Byron
committed
<?php
/**
* @file
Angie Byron
committed
* Contains \Drupal\comment\Form\ConfirmDeleteMultiple.
Angie Byron
committed
*/
namespace Drupal\comment\Form;
catch
committed
use Drupal\comment\CommentStorageInterface;
Angie Byron
committed
use Drupal\Component\Utility\String;
use Drupal\Core\Form\ConfirmFormBase;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
Angie Byron
committed
use Drupal\Core\Url;
Angie Byron
committed
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides the comment multiple delete confirmation form.
*/
Angie Byron
committed
class ConfirmDeleteMultiple extends ConfirmFormBase {
Angie Byron
committed
/**
* The comment storage.
*
catch
committed
* @var \Drupal\comment\CommentStorageInterface
Angie Byron
committed
*/
protected $commentStorage;
/**
* An array of comments to be deleted.
*
* @var \Drupal\comment\CommentInterface[]
*/
protected $comments;
/**
* Creates an new ConfirmDeleteMultiple form.
*
catch
committed
* @param \Drupal\comment\CommentStorageInterface $comment_storage
Angie Byron
committed
* The comment storage.
*/
catch
committed
public function __construct(CommentStorageInterface $comment_storage) {
Angie Byron
committed
$this->commentStorage = $comment_storage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
catch
committed
$container->get('entity.manager')->getStorage('comment')
Angie Byron
committed
);
}
/**
* {@inheritdoc}
*/
Alex Pott
committed
public function getFormId() {
Angie Byron
committed
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 getCancelUrl() {
Angie Byron
committed
return new Url('comment.admin');
Angie Byron
committed
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete comments');
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function buildForm(array $form, FormStateInterface $form_state) {
Angie Byron
committed
$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.'));
Angie Byron
committed
$form_state['redirect_route']['route_name'] = 'comment.admin';
Angie Byron
committed
}
Angie Byron
committed
return parent::buildForm($form, $form_state);
Angie Byron
committed
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function submitForm(array &$form, FormStateInterface $form_state) {
Angie Byron
committed
if ($form_state['values']['confirm']) {
$this->commentStorage->delete($this->comments);
$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_route'] = $this->getCancelUrl();