3 namespace Drupal\comment\Form;
5 use Drupal\comment\CommentInterface;
6 use Drupal\Component\Utility\Unicode;
7 use Drupal\Core\Datetime\DateFormatterInterface;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Extension\ModuleHandlerInterface;
10 use Drupal\Core\Form\FormBase;
11 use Drupal\Core\Form\FormStateInterface;
12 use Drupal\user\PrivateTempStoreFactory;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Provides the comments overview administration form.
18 class CommentAdminOverview extends FormBase {
21 * The entity type manager.
23 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
25 protected $entityTypeManager;
28 * The comment storage.
30 * @var \Drupal\comment\CommentStorageInterface
32 protected $commentStorage;
35 * The date formatter service.
37 * @var \Drupal\Core\Datetime\DateFormatterInterface
39 protected $dateFormatter;
44 * @var \Drupal\Core\Extension\ModuleHandlerInterface
46 protected $moduleHandler;
49 * The tempstore factory.
51 * @var \Drupal\user\PrivateTempStoreFactory
53 protected $tempStoreFactory;
56 * Creates a CommentAdminOverview form.
58 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
59 * The entity manager service.
60 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
61 * The date formatter service.
62 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
64 * @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
65 * The tempstore factory.
67 public function __construct(EntityTypeManagerInterface $entity_type_manager, DateFormatterInterface $date_formatter, ModuleHandlerInterface $module_handler, PrivateTempStoreFactory $temp_store_factory) {
68 $this->entityTypeManager = $entity_type_manager;
69 $this->commentStorage = $entity_type_manager->getStorage('comment');
70 $this->dateFormatter = $date_formatter;
71 $this->moduleHandler = $module_handler;
72 $this->tempStoreFactory = $temp_store_factory;
78 public static function create(ContainerInterface $container) {
80 $container->get('entity_type.manager'),
81 $container->get('date.formatter'),
82 $container->get('module_handler'),
83 $container->get('user.private_tempstore')
90 public function getFormId() {
91 return 'comment_admin_overview';
95 * Form constructor for the comment overview administration form.
98 * An associative array containing the structure of the form.
99 * @param \Drupal\Core\Form\FormStateInterface $form_state
100 * The current state of the form.
101 * @param string $type
102 * The type of the overview form ('approval' or 'new').
105 * The form structure.
107 public function buildForm(array $form, FormStateInterface $form_state, $type = 'new') {
109 // Build an 'Update options' form.
111 '#type' => 'details',
112 '#title' => $this->t('Update options'),
114 '#attributes' => ['class' => ['container-inline']],
117 if ($type == 'approval') {
118 $options['publish'] = $this->t('Publish the selected comments');
121 $options['unpublish'] = $this->t('Unpublish the selected comments');
123 $options['delete'] = $this->t('Delete the selected comments');
125 $form['options']['operation'] = [
127 '#title' => $this->t('Action'),
128 '#title_display' => 'invisible',
129 '#options' => $options,
130 '#default_value' => 'publish',
132 $form['options']['submit'] = [
134 '#value' => $this->t('Update'),
137 // Load the comments that need to be displayed.
138 $status = ($type == 'approval') ? CommentInterface::NOT_PUBLISHED : CommentInterface::PUBLISHED;
141 'data' => $this->t('Subject'),
142 'specifier' => 'subject',
145 'data' => $this->t('Author'),
146 'specifier' => 'name',
147 'class' => [RESPONSIVE_PRIORITY_MEDIUM],
150 'data' => $this->t('Posted in'),
151 'class' => [RESPONSIVE_PRIORITY_LOW],
154 'data' => $this->t('Updated'),
155 'specifier' => 'changed',
157 'class' => [RESPONSIVE_PRIORITY_LOW],
159 'operations' => $this->t('Operations'),
161 $cids = $this->commentStorage->getQuery()
162 ->condition('status', $status)
167 /** @var $comments \Drupal\comment\CommentInterface[] */
168 $comments = $this->commentStorage->loadMultiple($cids);
170 // Build a table listing the appropriate comments.
172 $destination = $this->getDestinationArray();
174 $commented_entity_ids = [];
175 $commented_entities = [];
177 foreach ($comments as $comment) {
178 $commented_entity_ids[$comment->getCommentedEntityTypeId()][] = $comment->getCommentedEntityId();
181 foreach ($commented_entity_ids as $entity_type => $ids) {
182 $commented_entities[$entity_type] = $this->entityTypeManager
183 ->getStorage($entity_type)
184 ->loadMultiple($ids);
187 foreach ($comments as $comment) {
188 /** @var $commented_entity \Drupal\Core\Entity\EntityInterface */
189 $commented_entity = $commented_entities[$comment->getCommentedEntityTypeId()][$comment->getCommentedEntityId()];
190 $comment_permalink = $comment->permalink();
191 if ($comment->hasField('comment_body') && ($body = $comment->get('comment_body')->value)) {
192 $attributes = $comment_permalink->getOption('attributes') ?: [];
193 $attributes += ['title' => Unicode::truncate($body, 128)];
194 $comment_permalink->setOption('attributes', $attributes);
196 $options[$comment->id()] = [
197 'title' => ['data' => ['#title' => $comment->getSubject() ?: $comment->id()]],
201 '#title' => $comment->getSubject(),
202 '#url' => $comment_permalink,
207 '#theme' => 'username',
208 '#account' => $comment->getOwner(),
214 '#title' => $commented_entity->label(),
215 '#access' => $commented_entity->access('view'),
216 '#url' => $commented_entity->urlInfo(),
219 'changed' => $this->dateFormatter->format($comment->getChangedTimeAcrossTranslations(), 'short'),
221 $comment_uri_options = $comment->urlInfo()->getOptions() + ['query' => $destination];
224 'title' => $this->t('Edit'),
225 'url' => $comment->urlInfo('edit-form', $comment_uri_options),
227 if ($this->moduleHandler->moduleExists('content_translation') && $this->moduleHandler->invoke('content_translation', 'translate_access', [$comment])->isAllowed()) {
228 $links['translate'] = [
229 'title' => $this->t('Translate'),
230 'url' => $comment->urlInfo('drupal:content-translation-overview', $comment_uri_options),
233 $options[$comment->id()]['operations']['data'] = [
234 '#type' => 'operations',
239 $form['comments'] = [
240 '#type' => 'tableselect',
241 '#header' => $header,
242 '#options' => $options,
243 '#empty' => $this->t('No comments available.'),
246 $form['pager'] = ['#type' => 'pager'];
254 public function validateForm(array &$form, FormStateInterface $form_state) {
255 $form_state->setValue('comments', array_diff($form_state->getValue('comments'), [0]));
256 // We can't execute any 'Update options' if no comments were selected.
257 if (count($form_state->getValue('comments')) == 0) {
258 $form_state->setErrorByName('', $this->t('Select one or more comments to perform the update on.'));
265 public function submitForm(array &$form, FormStateInterface $form_state) {
266 $operation = $form_state->getValue('operation');
267 $cids = $form_state->getValue('comments');
268 /** @var \Drupal\comment\CommentInterface[] $comments */
269 $comments = $this->commentStorage->loadMultiple($cids);
270 if ($operation != 'delete') {
271 foreach ($comments as $comment) {
272 if ($operation == 'unpublish') {
273 $comment->setUnpublished();
275 elseif ($operation == 'publish') {
276 $comment->setPublished();
280 drupal_set_message($this->t('The update has been performed.'));
281 $form_state->setRedirect('comment.admin');
285 /** @var \Drupal\comment\CommentInterface $comment */
286 foreach ($comments as $comment) {
287 $langcode = $comment->language()->getId();
288 $info[$comment->id()][$langcode] = $langcode;
290 $this->tempStoreFactory
291 ->get('comment_multiple_delete_confirm')
292 ->set($this->currentUser()->id(), $info);
293 $form_state->setRedirect('comment.multiple_delete_confirm');