PageRenderTime 28ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/mohamed_hussein/prodt
PHP | 300 lines | 197 code | 28 blank | 75 comment | 16 complexity | cca82653a8e8fed01d1e5a1f6e01a6c9 MD5 | raw file
  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 type 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. ->accessCheck(TRUE)
  149. ->condition('status', $status)
  150. ->tableSort($header)
  151. ->pager(50)
  152. ->execute();
  153. /** @var \Drupal\comment\CommentInterface[] $comments */
  154. $comments = $this->commentStorage->loadMultiple($cids);
  155. // Build a table listing the appropriate comments.
  156. $options = [];
  157. $destination = $this->getDestinationArray();
  158. $commented_entity_ids = [];
  159. $commented_entities = [];
  160. foreach ($comments as $comment) {
  161. $commented_entity_ids[$comment->getCommentedEntityTypeId()][] = $comment->getCommentedEntityId();
  162. }
  163. foreach ($commented_entity_ids as $entity_type => $ids) {
  164. $commented_entities[$entity_type] = $this->entityTypeManager
  165. ->getStorage($entity_type)
  166. ->loadMultiple($ids);
  167. }
  168. foreach ($comments as $comment) {
  169. /** @var \Drupal\Core\Entity\EntityInterface $commented_entity */
  170. $commented_entity = $commented_entities[$comment->getCommentedEntityTypeId()][$comment->getCommentedEntityId()];
  171. $comment_permalink = $comment->permalink();
  172. if ($comment->hasField('comment_body') && ($body = $comment->get('comment_body')->value)) {
  173. $attributes = $comment_permalink->getOption('attributes') ?: [];
  174. $attributes += ['title' => Unicode::truncate($body, 128)];
  175. $comment_permalink->setOption('attributes', $attributes);
  176. }
  177. $options[$comment->id()] = [
  178. 'title' => ['data' => ['#title' => $comment->getSubject() ?: $comment->id()]],
  179. 'subject' => [
  180. 'data' => [
  181. '#type' => 'link',
  182. '#title' => $comment->getSubject(),
  183. '#url' => $comment_permalink,
  184. ],
  185. ],
  186. 'author' => [
  187. 'data' => [
  188. '#theme' => 'username',
  189. '#account' => $comment->getOwner(),
  190. ],
  191. ],
  192. 'posted_in' => [
  193. 'data' => [
  194. '#type' => 'link',
  195. '#title' => $commented_entity->label(),
  196. '#access' => $commented_entity->access('view'),
  197. '#url' => $commented_entity->toUrl(),
  198. ],
  199. ],
  200. 'changed' => $this->dateFormatter->format($comment->getChangedTimeAcrossTranslations(), 'short'),
  201. ];
  202. $comment_uri_options = $comment->toUrl()->getOptions() + ['query' => $destination];
  203. $links = [];
  204. $links['edit'] = [
  205. 'title' => $this->t('Edit'),
  206. 'url' => $comment->toUrl('edit-form', $comment_uri_options),
  207. ];
  208. if ($this->moduleHandler->moduleExists('content_translation') && $this->moduleHandler->invoke('content_translation', 'translate_access', [$comment])->isAllowed()) {
  209. $links['translate'] = [
  210. 'title' => $this->t('Translate'),
  211. 'url' => $comment->toUrl('drupal:content-translation-overview', $comment_uri_options),
  212. ];
  213. }
  214. $options[$comment->id()]['operations']['data'] = [
  215. '#type' => 'operations',
  216. '#links' => $links,
  217. ];
  218. }
  219. $form['comments'] = [
  220. '#type' => 'tableselect',
  221. '#header' => $header,
  222. '#options' => $options,
  223. '#empty' => $this->t('No comments available.'),
  224. ];
  225. $form['pager'] = ['#type' => 'pager'];
  226. return $form;
  227. }
  228. /**
  229. * {@inheritdoc}
  230. */
  231. public function validateForm(array &$form, FormStateInterface $form_state) {
  232. $form_state->setValue('comments', array_diff($form_state->getValue('comments'), [0]));
  233. // We can't execute any 'Update options' if no comments were selected.
  234. if (count($form_state->getValue('comments')) == 0) {
  235. $form_state->setErrorByName('', $this->t('Select one or more comments to perform the update on.'));
  236. }
  237. }
  238. /**
  239. * {@inheritdoc}
  240. */
  241. public function submitForm(array &$form, FormStateInterface $form_state) {
  242. $operation = $form_state->getValue('operation');
  243. $cids = $form_state->getValue('comments');
  244. /** @var \Drupal\comment\CommentInterface[] $comments */
  245. $comments = $this->commentStorage->loadMultiple($cids);
  246. if ($operation != 'delete') {
  247. foreach ($comments as $comment) {
  248. if ($operation == 'unpublish') {
  249. $comment->setUnpublished();
  250. }
  251. elseif ($operation == 'publish') {
  252. $comment->setPublished();
  253. }
  254. $comment->save();
  255. }
  256. $this->messenger()->addStatus($this->t('The update has been performed.'));
  257. $form_state->setRedirect('comment.admin');
  258. }
  259. else {
  260. $info = [];
  261. /** @var \Drupal\comment\CommentInterface $comment */
  262. foreach ($comments as $comment) {
  263. $langcode = $comment->language()->getId();
  264. $info[$comment->id()][$langcode] = $langcode;
  265. }
  266. $this->tempStoreFactory
  267. ->get('entity_delete_multiple_confirm')
  268. ->set($this->currentUser()->id() . ':comment', $info);
  269. $form_state->setRedirect('entity.comment.delete_multiple_form');
  270. }
  271. }
  272. }