3 namespace Drupal\comment\Form;
5 use Drupal\comment\CommentStorageInterface;
6 use Drupal\Component\Utility\Html;
7 use Drupal\Core\Form\ConfirmFormBase;
8 use Drupal\Core\Form\FormStateInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Provides the comment multiple delete confirmation form.
15 class ConfirmDeleteMultiple extends ConfirmFormBase {
18 * The comment storage.
20 * @var \Drupal\comment\CommentStorageInterface
22 protected $commentStorage;
25 * An array of comments to be deleted.
27 * @var \Drupal\comment\CommentInterface[]
32 * Creates an new ConfirmDeleteMultiple form.
34 * @param \Drupal\comment\CommentStorageInterface $comment_storage
35 * The comment storage.
37 public function __construct(CommentStorageInterface $comment_storage) {
38 $this->commentStorage = $comment_storage;
44 public static function create(ContainerInterface $container) {
46 $container->get('entity.manager')->getStorage('comment')
53 public function getFormId() {
54 return 'comment_multiple_delete_confirm';
60 public function getQuestion() {
61 return $this->t('Are you sure you want to delete these comments and all their children?');
67 public function getCancelUrl() {
68 return new Url('comment.admin');
74 public function getConfirmText() {
75 return $this->t('Delete comments');
81 public function buildForm(array $form, FormStateInterface $form_state) {
82 $edit = $form_state->getUserInput();
89 // array_filter() returns only elements with actual values.
91 $this->comments = $this->commentStorage->loadMultiple(array_keys(array_filter($edit['comments'])));
92 foreach ($this->comments as $comment) {
93 $cid = $comment->id();
94 $form['comments'][$cid] = [
98 '#suffix' => Html::escape($comment->label()) . '</li>'
102 $form['operation'] = ['#type' => 'hidden', '#value' => 'delete'];
104 if (!$comment_counter) {
105 drupal_set_message($this->t('There do not appear to be any comments to delete, or your selected comment was deleted by another administrator.'));
106 $form_state->setRedirect('comment.admin');
109 return parent::buildForm($form, $form_state);
115 public function submitForm(array &$form, FormStateInterface $form_state) {
116 if ($form_state->getValue('confirm')) {
117 $this->commentStorage->delete($this->comments);
118 $count = count($form_state->getValue('comments'));
119 $this->logger('comment')->notice('Deleted @count comments.', ['@count' => $count]);
120 drupal_set_message($this->formatPlural($count, 'Deleted 1 comment.', 'Deleted @count comments.'));
122 $form_state->setRedirectUrl($this->getCancelUrl());