CommentLazyBuilders.php 7.7 KB

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