Version 1
[yaffs-website] / web / core / modules / comment / src / Form / ConfirmDeleteMultiple.php
1 <?php
2
3 namespace Drupal\comment\Form;
4
5 use Drupal\comment\CommentStorageInterface;
6 use Drupal\Component\Utility\Html;
7 use Drupal\Core\Form\ConfirmFormBase;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Url;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Provides the comment multiple delete confirmation form.
14  */
15 class ConfirmDeleteMultiple extends ConfirmFormBase {
16
17   /**
18    * The comment storage.
19    *
20    * @var \Drupal\comment\CommentStorageInterface
21    */
22   protected $commentStorage;
23
24   /**
25    * An array of comments to be deleted.
26    *
27    * @var \Drupal\comment\CommentInterface[]
28    */
29   protected $comments;
30
31   /**
32    * Creates an new ConfirmDeleteMultiple form.
33    *
34    * @param \Drupal\comment\CommentStorageInterface $comment_storage
35    *   The comment storage.
36    */
37   public function __construct(CommentStorageInterface $comment_storage) {
38     $this->commentStorage = $comment_storage;
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public static function create(ContainerInterface $container) {
45     return new static(
46       $container->get('entity.manager')->getStorage('comment')
47     );
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public function getFormId() {
54     return 'comment_multiple_delete_confirm';
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public function getQuestion() {
61     return $this->t('Are you sure you want to delete these comments and all their children?');
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function getCancelUrl() {
68     return new Url('comment.admin');
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function getConfirmText() {
75     return $this->t('Delete comments');
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function buildForm(array $form, FormStateInterface $form_state) {
82     $edit = $form_state->getUserInput();
83
84     $form['comments'] = [
85       '#prefix' => '<ul>',
86       '#suffix' => '</ul>',
87       '#tree' => TRUE,
88     ];
89     // array_filter() returns only elements with actual values.
90     $comment_counter = 0;
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] = [
95         '#type' => 'hidden',
96         '#value' => $cid,
97         '#prefix' => '<li>',
98         '#suffix' => Html::escape($comment->label()) . '</li>'
99       ];
100       $comment_counter++;
101     }
102     $form['operation'] = ['#type' => 'hidden', '#value' => 'delete'];
103
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');
107     }
108
109     return parent::buildForm($form, $form_state);
110   }
111
112   /**
113    * {@inheritdoc}
114    */
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.'));
121     }
122     $form_state->setRedirectUrl($this->getCancelUrl());
123   }
124
125 }