CommentLazyBuilders.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace Drupal\comment;
  3. use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
  4. use Drupal\Core\Entity\EntityFormBuilderInterface;
  5. use Drupal\Core\Entity\EntityInterface;
  6. use Drupal\Core\Entity\EntityManagerInterface;
  7. use Drupal\Core\Extension\ModuleHandlerInterface;
  8. use Drupal\Core\Render\RendererInterface;
  9. use Drupal\Core\Session\AccountInterface;
  10. use Drupal\Core\Url;
  11. /**
  12. * Defines a service for comment #lazy_builder callbacks.
  13. */
  14. class CommentLazyBuilders {
  15. /**
  16. * The entity manager service.
  17. *
  18. * @var \Drupal\Core\Entity\EntityManagerInterface
  19. */
  20. protected $entityManager;
  21. /**
  22. * The entity form builder service.
  23. *
  24. * @var \Drupal\Core\Entity\EntityFormBuilderInterface
  25. */
  26. protected $entityFormBuilder;
  27. /**
  28. * Comment manager service.
  29. *
  30. * @var \Drupal\comment\CommentManagerInterface
  31. */
  32. protected $commentManager;
  33. /**
  34. * Current logged in user.
  35. *
  36. * @var \Drupal\Core\Session\AccountInterface
  37. */
  38. protected $currentUser;
  39. /**
  40. * The module handler service.
  41. *
  42. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  43. */
  44. protected $moduleHandler;
  45. /**
  46. * The renderer service.
  47. *
  48. * @var \Drupal\Core\Render\RendererInterface
  49. */
  50. protected $renderer;
  51. /**
  52. * Constructs a new CommentLazyBuilders object.
  53. *
  54. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
  55. * The entity manager service.
  56. * @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder
  57. * The entity form builder service.
  58. * @param \Drupal\Core\Session\AccountInterface $current_user
  59. * The current logged in user.
  60. * @param \Drupal\comment\CommentManagerInterface $comment_manager
  61. * The comment manager service.
  62. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  63. * The module handler service.
  64. * @param \Drupal\Core\Render\RendererInterface $renderer
  65. * The renderer service.
  66. */
  67. public function __construct(EntityManagerInterface $entity_manager, EntityFormBuilderInterface $entity_form_builder, AccountInterface $current_user, CommentManagerInterface $comment_manager, ModuleHandlerInterface $module_handler, RendererInterface $renderer) {
  68. $this->entityManager = $entity_manager;
  69. $this->entityFormBuilder = $entity_form_builder;
  70. $this->currentUser = $current_user;
  71. $this->commentManager = $comment_manager;
  72. $this->moduleHandler = $module_handler;
  73. $this->renderer = $renderer;
  74. }
  75. /**
  76. * #lazy_builder callback; builds the comment form.
  77. *
  78. * @param string $commented_entity_type_id
  79. * The commented entity type ID.
  80. * @param string $commented_entity_id
  81. * The commented entity ID.
  82. * @param string $field_name
  83. * The comment field name.
  84. * @param string $comment_type_id
  85. * The comment type ID.
  86. *
  87. * @return array
  88. * A renderable array containing the comment form.
  89. */
  90. public function renderForm($commented_entity_type_id, $commented_entity_id, $field_name, $comment_type_id) {
  91. $values = [
  92. 'entity_type' => $commented_entity_type_id,
  93. 'entity_id' => $commented_entity_id,
  94. 'field_name' => $field_name,
  95. 'comment_type' => $comment_type_id,
  96. 'pid' => NULL,
  97. ];
  98. $comment = $this->entityManager->getStorage('comment')->create($values);
  99. return $this->entityFormBuilder->getForm($comment);
  100. }
  101. /**
  102. * #lazy_builder callback; builds a comment's links.
  103. *
  104. * @param string $comment_entity_id
  105. * The comment entity ID.
  106. * @param string $view_mode
  107. * The view mode in which the comment entity is being viewed.
  108. * @param string $langcode
  109. * The language in which the comment entity is being viewed.
  110. * @param bool $is_in_preview
  111. * Whether the comment is currently being previewed.
  112. *
  113. * @return array
  114. * A renderable array representing the comment links.
  115. */
  116. public function renderLinks($comment_entity_id, $view_mode, $langcode, $is_in_preview) {
  117. $links = [
  118. '#theme' => 'links__comment',
  119. '#pre_render' => ['drupal_pre_render_links'],
  120. '#attributes' => ['class' => ['links', 'inline']],
  121. ];
  122. if (!$is_in_preview) {
  123. /** @var \Drupal\comment\CommentInterface $entity */
  124. $entity = $this->entityManager->getStorage('comment')->load($comment_entity_id);
  125. $commented_entity = $entity->getCommentedEntity();
  126. $links['comment'] = $this->buildLinks($entity, $commented_entity);
  127. // Allow other modules to alter the comment links.
  128. $hook_context = [
  129. 'view_mode' => $view_mode,
  130. 'langcode' => $langcode,
  131. 'commented_entity' => $commented_entity,
  132. ];
  133. $this->moduleHandler->alter('comment_links', $links, $entity, $hook_context);
  134. }
  135. return $links;
  136. }
  137. /**
  138. * Build the default links (reply, edit, delete …) for a comment.
  139. *
  140. * @param \Drupal\comment\CommentInterface $entity
  141. * The comment object.
  142. * @param \Drupal\Core\Entity\EntityInterface $commented_entity
  143. * The entity to which the comment is attached.
  144. *
  145. * @return array
  146. * An array that can be processed by drupal_pre_render_links().
  147. */
  148. protected function buildLinks(CommentInterface $entity, EntityInterface $commented_entity) {
  149. $links = [];
  150. $status = $commented_entity->get($entity->getFieldName())->status;
  151. if ($status == CommentItemInterface::OPEN) {
  152. if ($entity->access('delete')) {
  153. $links['comment-delete'] = [
  154. 'title' => t('Delete'),
  155. 'url' => $entity->urlInfo('delete-form'),
  156. ];
  157. }
  158. if ($entity->access('update')) {
  159. $links['comment-edit'] = [
  160. 'title' => t('Edit'),
  161. 'url' => $entity->urlInfo('edit-form'),
  162. ];
  163. }
  164. if ($entity->access('create')) {
  165. $links['comment-reply'] = [
  166. 'title' => t('Reply'),
  167. 'url' => Url::fromRoute('comment.reply', [
  168. 'entity_type' => $entity->getCommentedEntityTypeId(),
  169. 'entity' => $entity->getCommentedEntityId(),
  170. 'field_name' => $entity->getFieldName(),
  171. 'pid' => $entity->id(),
  172. ]),
  173. ];
  174. }
  175. if (!$entity->isPublished() && $entity->access('approve')) {
  176. $links['comment-approve'] = [
  177. 'title' => t('Approve'),
  178. 'url' => Url::fromRoute('comment.approve', ['comment' => $entity->id()]),
  179. ];
  180. }
  181. if (empty($links) && $this->currentUser->isAnonymous()) {
  182. $links['comment-forbidden']['title'] = $this->commentManager->forbiddenMessage($commented_entity, $entity->getFieldName());
  183. }
  184. }
  185. // Add translations link for translation-enabled comment bundles.
  186. if ($this->moduleHandler->moduleExists('content_translation') && $this->access($entity)->isAllowed()) {
  187. $links['comment-translations'] = [
  188. 'title' => t('Translate'),
  189. 'url' => $entity->urlInfo('drupal:content-translation-overview'),
  190. ];
  191. }
  192. return [
  193. '#theme' => 'links__comment__comment',
  194. // The "entity" property is specified to be present, so no need to check.
  195. '#links' => $links,
  196. '#attributes' => ['class' => ['links', 'inline']],
  197. ];
  198. }
  199. /**
  200. * Wraps content_translation_translate_access.
  201. */
  202. protected function access(EntityInterface $entity) {
  203. return content_translation_translate_access($entity);
  204. }
  205. }