ConfirmDeleteMultiple.php in Zircon Profile 8
File
core/modules/comment/src/Form/ConfirmDeleteMultiple.php
View source
<?php
namespace Drupal\comment\Form;
use Drupal\comment\CommentStorageInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ConfirmDeleteMultiple extends ConfirmFormBase {
protected $commentStorage;
protected $comments;
public function __construct(CommentStorageInterface $comment_storage) {
$this->commentStorage = $comment_storage;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.manager')
->getStorage('comment'));
}
public function getFormId() {
return 'comment_multiple_delete_confirm';
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete these comments and all their children?');
}
public function getCancelUrl() {
return new Url('comment.admin');
}
public function getConfirmText() {
return $this
->t('Delete comments');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$edit = $form_state
->getUserInput();
$form['comments'] = array(
'#prefix' => '<ul>',
'#suffix' => '</ul>',
'#tree' => TRUE,
);
$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' => Html::escape($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
->setRedirect('comment.admin');
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getValue('confirm')) {
$this->commentStorage
->delete($this->comments);
$count = count($form_state
->getValue('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());
}
}