PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/comment/src/Form/CommentAdminOverview.php

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