CommentManager.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace Drupal\comment;
  3. use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
  4. use Drupal\Core\Config\ConfigFactoryInterface;
  5. use Drupal\Core\Entity\EntityInterface;
  6. use Drupal\Core\Entity\EntityManagerInterface;
  7. use Drupal\Core\Entity\FieldableEntityInterface;
  8. use Drupal\Core\Extension\ModuleHandlerInterface;
  9. use Drupal\Core\Routing\UrlGeneratorInterface;
  10. use Drupal\Core\Routing\UrlGeneratorTrait;
  11. use Drupal\Core\Session\AccountInterface;
  12. use Drupal\Core\StringTranslation\StringTranslationTrait;
  13. use Drupal\Core\StringTranslation\TranslationInterface;
  14. use Drupal\field\Entity\FieldStorageConfig;
  15. use Drupal\field\Entity\FieldConfig;
  16. use Drupal\user\RoleInterface;
  17. /**
  18. * Comment manager contains common functions to manage comment fields.
  19. */
  20. class CommentManager implements CommentManagerInterface {
  21. use StringTranslationTrait;
  22. use UrlGeneratorTrait;
  23. /**
  24. * The entity manager service.
  25. *
  26. * @var \Drupal\Core\Entity\EntityManagerInterface
  27. */
  28. protected $entityManager;
  29. /**
  30. * Whether the \Drupal\user\RoleInterface::AUTHENTICATED_ID can post comments.
  31. *
  32. * @var bool
  33. */
  34. protected $authenticatedCanPostComments;
  35. /**
  36. * The user settings config object.
  37. *
  38. * @var \Drupal\Core\Config\Config
  39. */
  40. protected $userConfig;
  41. /**
  42. * The module handler service.
  43. *
  44. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  45. */
  46. protected $moduleHandler;
  47. /**
  48. * The current user.
  49. *
  50. * @var \Drupal\Core\Session\AccountInterface
  51. */
  52. protected $currentUser;
  53. /**
  54. * Construct the CommentManager object.
  55. *
  56. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
  57. * The entity manager service.
  58. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  59. * The config factory.
  60. * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
  61. * The string translation service.
  62. * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
  63. * The url generator service.
  64. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  65. * The module handler service.
  66. * @param \Drupal\Core\Session\AccountInterface $current_user
  67. * The current user.
  68. */
  69. public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, TranslationInterface $string_translation, UrlGeneratorInterface $url_generator, ModuleHandlerInterface $module_handler, AccountInterface $current_user) {
  70. $this->entityManager = $entity_manager;
  71. $this->userConfig = $config_factory->get('user.settings');
  72. $this->stringTranslation = $string_translation;
  73. $this->urlGenerator = $url_generator;
  74. $this->moduleHandler = $module_handler;
  75. $this->currentUser = $current_user;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function getFields($entity_type_id) {
  81. $entity_type = $this->entityManager->getDefinition($entity_type_id);
  82. if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) {
  83. return [];
  84. }
  85. $map = $this->entityManager->getFieldMapByFieldType('comment');
  86. return isset($map[$entity_type_id]) ? $map[$entity_type_id] : [];
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function addBodyField($comment_type_id) {
  92. if (!FieldConfig::loadByName('comment', $comment_type_id, 'comment_body')) {
  93. // Attaches the body field by default.
  94. $field = $this->entityManager->getStorage('field_config')->create([
  95. 'label' => 'Comment',
  96. 'bundle' => $comment_type_id,
  97. 'required' => TRUE,
  98. 'field_storage' => FieldStorageConfig::loadByName('comment', 'comment_body'),
  99. ]);
  100. $field->save();
  101. // Assign widget settings for the 'default' form mode.
  102. entity_get_form_display('comment', $comment_type_id, 'default')
  103. ->setComponent('comment_body', [
  104. 'type' => 'text_textarea',
  105. ])
  106. ->save();
  107. // Assign display settings for the 'default' view mode.
  108. entity_get_display('comment', $comment_type_id, 'default')
  109. ->setComponent('comment_body', [
  110. 'label' => 'hidden',
  111. 'type' => 'text_default',
  112. 'weight' => 0,
  113. ])
  114. ->save();
  115. }
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function forbiddenMessage(EntityInterface $entity, $field_name) {
  121. if (!isset($this->authenticatedCanPostComments)) {
  122. // We only output a link if we are certain that users will get the
  123. // permission to post comments by logging in.
  124. $this->authenticatedCanPostComments = $this->entityManager
  125. ->getStorage('user_role')
  126. ->load(RoleInterface::AUTHENTICATED_ID)
  127. ->hasPermission('post comments');
  128. }
  129. if ($this->authenticatedCanPostComments) {
  130. // We cannot use the redirect.destination service here because these links
  131. // sometimes appear on /node and taxonomy listing pages.
  132. if ($entity->get($field_name)->getFieldDefinition()->getSetting('form_location') == CommentItemInterface::FORM_SEPARATE_PAGE) {
  133. $comment_reply_parameters = [
  134. 'entity_type' => $entity->getEntityTypeId(),
  135. 'entity' => $entity->id(),
  136. 'field_name' => $field_name,
  137. ];
  138. $destination = ['destination' => $this->url('comment.reply', $comment_reply_parameters, ['fragment' => 'comment-form'])];
  139. }
  140. else {
  141. $destination = ['destination' => $entity->url('canonical', ['fragment' => 'comment-form'])];
  142. }
  143. if ($this->userConfig->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) {
  144. // Users can register themselves.
  145. return $this->t('<a href=":login">Log in</a> or <a href=":register">register</a> to post comments', [
  146. ':login' => $this->urlGenerator->generateFromRoute('user.login', [], ['query' => $destination]),
  147. ':register' => $this->urlGenerator->generateFromRoute('user.register', [], ['query' => $destination]),
  148. ]);
  149. }
  150. else {
  151. // Only admins can add new users, no public registration.
  152. return $this->t('<a href=":login">Log in</a> to post comments', [
  153. ':login' => $this->urlGenerator->generateFromRoute('user.login', [], ['query' => $destination]),
  154. ]);
  155. }
  156. }
  157. return '';
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. public function getCountNewComments(EntityInterface $entity, $field_name = NULL, $timestamp = 0) {
  163. // @todo Replace module handler with optional history service injection
  164. // after https://www.drupal.org/node/2081585.
  165. if ($this->currentUser->isAuthenticated() && $this->moduleHandler->moduleExists('history')) {
  166. // Retrieve the timestamp at which the current user last viewed this entity.
  167. if (!$timestamp) {
  168. if ($entity->getEntityTypeId() == 'node') {
  169. $timestamp = history_read($entity->id());
  170. }
  171. else {
  172. $function = $entity->getEntityTypeId() . '_last_viewed';
  173. if (function_exists($function)) {
  174. $timestamp = $function($entity->id());
  175. }
  176. else {
  177. // Default to 30 days ago.
  178. // @todo Remove once https://www.drupal.org/node/1029708 lands.
  179. $timestamp = COMMENT_NEW_LIMIT;
  180. }
  181. }
  182. }
  183. $timestamp = ($timestamp > HISTORY_READ_LIMIT ? $timestamp : HISTORY_READ_LIMIT);
  184. // Use the timestamp to retrieve the number of new comments.
  185. $query = $this->entityManager->getStorage('comment')->getQuery()
  186. ->condition('entity_type', $entity->getEntityTypeId())
  187. ->condition('entity_id', $entity->id())
  188. ->condition('created', $timestamp, '>')
  189. ->condition('status', CommentInterface::PUBLISHED);
  190. if ($field_name) {
  191. // Limit to a particular field.
  192. $query->condition('field_name', $field_name);
  193. }
  194. return $query->count()->execute();
  195. }
  196. return FALSE;
  197. }
  198. }