24f8b0aa2e6dad12d525cfd0860262edb98cf26c
[yaffs-website] / web / core / modules / comment / src / Form / CommentAdminOverview.php
1 <?php
2
3 namespace Drupal\comment\Form;
4
5 use Drupal\comment\CommentInterface;
6 use Drupal\comment\CommentStorageInterface;
7 use Drupal\Component\Utility\Unicode;
8 use Drupal\Core\Datetime\DateFormatterInterface;
9 use Drupal\Core\Entity\EntityManagerInterface;
10 use Drupal\Core\Extension\ModuleHandlerInterface;
11 use Drupal\Core\Form\FormBase;
12 use Drupal\Core\Form\FormStateInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14
15 /**
16  * Provides the comments overview administration form.
17  */
18 class CommentAdminOverview extends FormBase {
19
20   /**
21    * The entity storage.
22    *
23    * @var \Drupal\Core\Entity\EntityManagerInterface
24    */
25   protected $entityManager;
26
27   /**
28    * The comment storage.
29    *
30    * @var \Drupal\comment\CommentStorageInterface
31    */
32   protected $commentStorage;
33
34   /**
35    * The date formatter service.
36    *
37    * @var \Drupal\Core\Datetime\DateFormatterInterface
38    */
39   protected $dateFormatter;
40
41   /**
42    * The module handler.
43    *
44    * @var \Drupal\Core\Extension\ModuleHandlerInterface
45    */
46   protected $moduleHandler;
47
48   /**
49    * Creates a CommentAdminOverview form.
50    *
51    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
52    *   The entity manager service.
53    * @param \Drupal\comment\CommentStorageInterface $comment_storage
54    *   The comment storage.
55    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
56    *   The date formatter service.
57    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
58    *   The module handler.
59    */
60   public function __construct(EntityManagerInterface $entity_manager, CommentStorageInterface $comment_storage, DateFormatterInterface $date_formatter, ModuleHandlerInterface $module_handler) {
61     $this->entityManager = $entity_manager;
62     $this->commentStorage = $comment_storage;
63     $this->dateFormatter = $date_formatter;
64     $this->moduleHandler = $module_handler;
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public static function create(ContainerInterface $container) {
71     return new static(
72       $container->get('entity.manager'),
73       $container->get('entity.manager')->getStorage('comment'),
74       $container->get('date.formatter'),
75       $container->get('module_handler')
76     );
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public function getFormId() {
83     return 'comment_admin_overview';
84   }
85
86   /**
87    * Form constructor for the comment overview administration form.
88    *
89    * @param array $form
90    *   An associative array containing the structure of the form.
91    * @param \Drupal\Core\Form\FormStateInterface $form_state
92    *   The current state of the form.
93    * @param string $type
94    *   The type of the overview form ('approval' or 'new').
95    *
96    * @return array
97    *   The form structure.
98    */
99   public function buildForm(array $form, FormStateInterface $form_state, $type = 'new') {
100
101     // Build an 'Update options' form.
102     $form['options'] = [
103       '#type' => 'details',
104       '#title' => $this->t('Update options'),
105       '#open' => TRUE,
106       '#attributes' => ['class' => ['container-inline']],
107     ];
108
109     if ($type == 'approval') {
110       $options['publish'] = $this->t('Publish the selected comments');
111     }
112     else {
113       $options['unpublish'] = $this->t('Unpublish the selected comments');
114     }
115     $options['delete'] = $this->t('Delete the selected comments');
116
117     $form['options']['operation'] = [
118       '#type' => 'select',
119       '#title' => $this->t('Action'),
120       '#title_display' => 'invisible',
121       '#options' => $options,
122       '#default_value' => 'publish',
123     ];
124     $form['options']['submit'] = [
125       '#type' => 'submit',
126       '#value' => $this->t('Update'),
127     ];
128
129     // Load the comments that need to be displayed.
130     $status = ($type == 'approval') ? CommentInterface::NOT_PUBLISHED : CommentInterface::PUBLISHED;
131     $header = [
132       'subject' => [
133         'data' => $this->t('Subject'),
134         'specifier' => 'subject',
135       ],
136       'author' => [
137         'data' => $this->t('Author'),
138         'specifier' => 'name',
139         'class' => [RESPONSIVE_PRIORITY_MEDIUM],
140       ],
141       'posted_in' => [
142         'data' => $this->t('Posted in'),
143         'class' => [RESPONSIVE_PRIORITY_LOW],
144       ],
145       'changed' => [
146         'data' => $this->t('Updated'),
147         'specifier' => 'changed',
148         'sort' => 'desc',
149         'class' => [RESPONSIVE_PRIORITY_LOW],
150       ],
151       'operations' => $this->t('Operations'),
152     ];
153     $cids = $this->commentStorage->getQuery()
154       ->condition('status', $status)
155       ->tableSort($header)
156       ->pager(50)
157       ->execute();
158
159     /** @var $comments \Drupal\comment\CommentInterface[] */
160     $comments = $this->commentStorage->loadMultiple($cids);
161
162     // Build a table listing the appropriate comments.
163     $options = [];
164     $destination = $this->getDestinationArray();
165
166     $commented_entity_ids = [];
167     $commented_entities = [];
168
169     foreach ($comments as $comment) {
170       $commented_entity_ids[$comment->getCommentedEntityTypeId()][] = $comment->getCommentedEntityId();
171     }
172
173     foreach ($commented_entity_ids as $entity_type => $ids) {
174       $commented_entities[$entity_type] = $this->entityManager->getStorage($entity_type)->loadMultiple($ids);
175     }
176
177     foreach ($comments as $comment) {
178       /** @var $commented_entity \Drupal\Core\Entity\EntityInterface */
179       $commented_entity = $commented_entities[$comment->getCommentedEntityTypeId()][$comment->getCommentedEntityId()];
180       $comment_permalink = $comment->permalink();
181       if ($comment->hasField('comment_body') && ($body = $comment->get('comment_body')->value)) {
182         $attributes = $comment_permalink->getOption('attributes') ?: [];
183         $attributes += ['title' => Unicode::truncate($body, 128)];
184         $comment_permalink->setOption('attributes', $attributes);
185       }
186       $options[$comment->id()] = [
187         'title' => ['data' => ['#title' => $comment->getSubject() ?: $comment->id()]],
188         'subject' => [
189           'data' => [
190             '#type' => 'link',
191             '#title' => $comment->getSubject(),
192             '#url' => $comment_permalink,
193           ],
194         ],
195         'author' => [
196           'data' => [
197             '#theme' => 'username',
198             '#account' => $comment->getOwner(),
199           ],
200         ],
201         'posted_in' => [
202           'data' => [
203             '#type' => 'link',
204             '#title' => $commented_entity->label(),
205             '#access' => $commented_entity->access('view'),
206             '#url' => $commented_entity->urlInfo(),
207           ],
208         ],
209         'changed' => $this->dateFormatter->format($comment->getChangedTimeAcrossTranslations(), 'short'),
210       ];
211       $comment_uri_options = $comment->urlInfo()->getOptions() + ['query' => $destination];
212       $links = [];
213       $links['edit'] = [
214         'title' => $this->t('Edit'),
215         'url' => $comment->urlInfo('edit-form', $comment_uri_options),
216       ];
217       if ($this->moduleHandler->moduleExists('content_translation') && $this->moduleHandler->invoke('content_translation', 'translate_access', [$comment])->isAllowed()) {
218         $links['translate'] = [
219           'title' => $this->t('Translate'),
220           'url' => $comment->urlInfo('drupal:content-translation-overview', $comment_uri_options),
221         ];
222       }
223       $options[$comment->id()]['operations']['data'] = [
224         '#type' => 'operations',
225         '#links' => $links,
226       ];
227     }
228
229     $form['comments'] = [
230       '#type' => 'tableselect',
231       '#header' => $header,
232       '#options' => $options,
233       '#empty' => $this->t('No comments available.'),
234     ];
235
236     $form['pager'] = ['#type' => 'pager'];
237
238     return $form;
239   }
240
241   /**
242    * {@inheritdoc}
243    */
244   public function validateForm(array &$form, FormStateInterface $form_state) {
245     $form_state->setValue('comments', array_diff($form_state->getValue('comments'), [0]));
246     // We can't execute any 'Update options' if no comments were selected.
247     if (count($form_state->getValue('comments')) == 0) {
248       $form_state->setErrorByName('', $this->t('Select one or more comments to perform the update on.'));
249     }
250   }
251
252   /**
253    * {@inheritdoc}
254    */
255   public function submitForm(array &$form, FormStateInterface $form_state) {
256     $operation = $form_state->getValue('operation');
257     $cids = $form_state->getValue('comments');
258
259     foreach ($cids as $cid) {
260       // Delete operation handled in \Drupal\comment\Form\ConfirmDeleteMultiple
261       // see \Drupal\comment\Controller\AdminController::adminPage().
262       if ($operation == 'unpublish') {
263         $comment = $this->commentStorage->load($cid);
264         $comment->setPublished(FALSE);
265         $comment->save();
266       }
267       elseif ($operation == 'publish') {
268         $comment = $this->commentStorage->load($cid);
269         $comment->setPublished(TRUE);
270         $comment->save();
271       }
272     }
273     drupal_set_message($this->t('The update has been performed.'));
274     $form_state->setRedirect('comment.admin');
275   }
276
277 }