comment.module

Enables users to comment on published content.

When enabled, the Comment module creates a discussion board for each Drupal node. Users can post comments to discuss a forum topic, story, collaborative book page, etc.

File

drupal/core/modules/comment/comment.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Enables users to comment on published content.
  5. *
  6. * When enabled, the Comment module creates a discussion board for each Drupal
  7. * node. Users can post comments to discuss a forum topic, story, collaborative
  8. * book page, etc.
  9. */
  10. use Drupal\entity\Plugin\Core\Entity\EntityDisplay;
  11. use Drupal\file\Plugin\Core\Entity\File;
  12. use Drupal\Core\Entity\EntityInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. use Symfony\Component\HttpKernel\HttpKernelInterface;
  16. /**
  17. * Comment is awaiting approval.
  18. */
  19. const COMMENT_NOT_PUBLISHED = 0;
  20. /**
  21. * Comment is published.
  22. */
  23. const COMMENT_PUBLISHED = 1;
  24. /**
  25. * Comments are displayed in a flat list - expanded.
  26. */
  27. const COMMENT_MODE_FLAT = 0;
  28. /**
  29. * Comments are displayed as a threaded list - expanded.
  30. */
  31. const COMMENT_MODE_THREADED = 1;
  32. /**
  33. * Anonymous posters cannot enter their contact information.
  34. */
  35. const COMMENT_ANONYMOUS_MAYNOT_CONTACT = 0;
  36. /**
  37. * Anonymous posters may leave their contact information.
  38. */
  39. const COMMENT_ANONYMOUS_MAY_CONTACT = 1;
  40. /**
  41. * Anonymous posters are required to leave their contact information.
  42. */
  43. const COMMENT_ANONYMOUS_MUST_CONTACT = 2;
  44. /**
  45. * Comment form should be displayed on a separate page.
  46. */
  47. const COMMENT_FORM_SEPARATE_PAGE = 0;
  48. /**
  49. * Comment form should be shown below post or list of comments.
  50. */
  51. const COMMENT_FORM_BELOW = 1;
  52. /**
  53. * Comments for this node are hidden.
  54. */
  55. const COMMENT_NODE_HIDDEN = 0;
  56. /**
  57. * Comments for this node are closed.
  58. */
  59. const COMMENT_NODE_CLOSED = 1;
  60. /**
  61. * Comments for this node are open.
  62. */
  63. const COMMENT_NODE_OPEN = 2;
  64. use Drupal\comment\Plugin\Core\Entity\Comment;
  65. /**
  66. * Implements hook_help().
  67. */
  68. function comment_help($path, $arg) {
  69. switch ($path) {
  70. case 'admin/help#comment':
  71. $output = '<h3>' . t('About') . '</h3>';
  72. $output .= '<p>' . t('The Comment module allows users to comment on site content, set commenting defaults and permissions, and moderate comments. For more information, see the online handbook entry for <a href="@comment">Comment module</a>.', array('@comment' => 'http://drupal.org/documentation/modules/comment')) . '</p>';
  73. $output .= '<h3>' . t('Uses') . '</h3>';
  74. $output .= '<dl>';
  75. $output .= '<dt>' . t('Default and custom settings') . '</dt>';
  76. $output .= '<dd>' . t("Each <a href='@content-type'>content type</a> can have its own default comment settings configured as: <em>Open</em> to allow new comments, <em>Hidden</em> to hide existing comments and prevent new comments, or <em>Closed</em> to view existing comments, but prevent new comments. These defaults will apply to all new content created (changes to the settings on existing content must be done manually). Other comment settings can also be customized per content type, and can be overridden for any given item of content. When a comment has no replies, it remains editable by its author, as long as the author has a user account and is logged in.", array('@content-type' => url('admin/structure/types'))) . '</dd>';
  77. $output .= '<dt>' . t('Comment approval') . '</dt>';
  78. $output .= '<dd>' . t("Comments from users who have the <em>Skip comment approval</em> permission are published immediately. All other comments are placed in the <a href='@comment-approval'>Unapproved comments</a> queue, until a user who has permission to <em>Administer comments</em> publishes or deletes them. Published comments can be bulk managed on the <a href='@admin-comment'>Published comments</a> administration page.", array('@comment-approval' => url('admin/content/comment/approval'), '@admin-comment' => url('admin/content/comment'))) . '</dd>';
  79. $output .= '</dl>';
  80. return $output;
  81. }
  82. }
  83. /**
  84. * Implements hook_entity_bundle_info().
  85. */
  86. function comment_entity_bundle_info() {
  87. $bundles = array();
  88. foreach (node_type_get_names() as $type => $name) {
  89. $bundles['comment']['comment_node_' . $type] = array(
  90. 'label' => t('@node_type comment', array('@node_type' => $name)),
  91. // Provide the node type/bundle name for other modules, so it does not
  92. // have to be extracted manually from the bundle name.
  93. 'node bundle' => $type,
  94. );
  95. }
  96. return $bundles;
  97. }
  98. /**
  99. * Loads the comment bundle name corresponding a given content type.
  100. *
  101. * This function is used as a menu loader callback in comment_menu().
  102. *
  103. * @param $name
  104. * The machine name of the node type whose comment fields are to be edited.
  105. *
  106. * @return
  107. * The comment bundle name corresponding to the node type.
  108. *
  109. * @see comment_menu_alter()
  110. */
  111. function comment_node_type_load($name) {
  112. if ($type = node_type_load($name)) {
  113. return 'comment_node_' . $type->type;
  114. }
  115. }
  116. /**
  117. * Entity URI callback.
  118. */
  119. function comment_uri(Comment $comment) {
  120. return array(
  121. 'path' => 'comment/' . $comment->id(),
  122. 'options' => array('fragment' => 'comment-' . $comment->id()),
  123. );
  124. }
  125. /**
  126. * Implements hook_field_extra_fields().
  127. */
  128. function comment_field_extra_fields() {
  129. $return = array();
  130. foreach (node_type_get_types() as $type) {
  131. if (variable_get('comment_subject_field_' . $type->type, 1) == 1) {
  132. $return['comment']['comment_node_' . $type->type] = array(
  133. 'form' => array(
  134. 'author' => array(
  135. 'label' => t('Author'),
  136. 'description' => t('Author textfield'),
  137. 'weight' => -2,
  138. ),
  139. 'subject' => array(
  140. 'label' => t('Subject'),
  141. 'description' => t('Subject textfield'),
  142. 'weight' => -1,
  143. ),
  144. ),
  145. );
  146. }
  147. }
  148. return $return;
  149. }
  150. /**
  151. * Implements hook_theme().
  152. */
  153. function comment_theme() {
  154. return array(
  155. 'comment_block' => array(
  156. 'variables' => array('number' => NULL),
  157. ),
  158. 'comment_preview' => array(
  159. 'variables' => array('comment' => NULL),
  160. ),
  161. 'comment' => array(
  162. 'template' => 'comment',
  163. 'render element' => 'elements',
  164. ),
  165. 'comment_post_forbidden' => array(
  166. 'variables' => array('node' => NULL),
  167. ),
  168. 'comment_wrapper' => array(
  169. 'template' => 'comment-wrapper',
  170. 'render element' => 'content',
  171. ),
  172. );
  173. }
  174. /**
  175. * Implements hook_menu().
  176. */
  177. function comment_menu() {
  178. $items['admin/content/comment'] = array(
  179. 'title' => 'Comments',
  180. 'description' => 'List and edit site comments and the comment approval queue.',
  181. 'page callback' => 'comment_admin',
  182. 'access arguments' => array('administer comments'),
  183. 'type' => MENU_LOCAL_TASK | MENU_NORMAL_ITEM,
  184. 'file' => 'comment.admin.inc',
  185. );
  186. // Tabs begin here.
  187. $items['admin/content/comment/new'] = array(
  188. 'title' => 'Published comments',
  189. 'type' => MENU_DEFAULT_LOCAL_TASK,
  190. );
  191. $items['admin/content/comment/approval'] = array(
  192. 'title' => 'Unapproved comments',
  193. 'title callback' => 'comment_count_unpublished',
  194. 'page arguments' => array('approval'),
  195. 'access arguments' => array('administer comments'),
  196. 'type' => MENU_LOCAL_TASK,
  197. );
  198. $items['comment/%comment'] = array(
  199. 'title' => 'Comment permalink',
  200. 'page callback' => 'comment_permalink',
  201. 'page arguments' => array(1),
  202. 'access callback' => 'entity_page_access',
  203. 'access arguments' => array(1, 'view'),
  204. );
  205. $items['comment/%comment/view'] = array(
  206. 'title' => 'View comment',
  207. 'type' => MENU_DEFAULT_LOCAL_TASK,
  208. );
  209. // Every other comment path uses %, but this one loads the comment directly,
  210. // so we don't end up loading it twice (in the page and access callback).
  211. $items['comment/%comment/edit'] = array(
  212. 'title' => 'Edit',
  213. 'type' => MENU_LOCAL_TASK,
  214. 'route_name' => 'comment_edit_page',
  215. );
  216. $items['comment/%comment/approve'] = array(
  217. 'title' => 'Approve',
  218. 'page callback' => 'comment_approve',
  219. 'page arguments' => array(1),
  220. 'access callback' => 'entity_page_access',
  221. 'access arguments' => array(1, 'approve'),
  222. 'file' => 'comment.pages.inc',
  223. 'weight' => 10,
  224. );
  225. $items['comment/%comment/delete'] = array(
  226. 'title' => 'Delete',
  227. 'page callback' => 'comment_confirm_delete_page',
  228. 'page arguments' => array(1),
  229. 'access callback' => 'entity_page_access',
  230. 'access arguments' => array(1, 'delete'),
  231. 'type' => MENU_LOCAL_TASK,
  232. 'file' => 'comment.admin.inc',
  233. 'weight' => 20,
  234. );
  235. $items['comment/reply/%node'] = array(
  236. 'title' => 'Add new comment',
  237. 'page callback' => 'comment_reply',
  238. 'page arguments' => array(2),
  239. 'access callback' => 'node_access',
  240. 'access arguments' => array('view', 2),
  241. 'file' => 'comment.pages.inc',
  242. );
  243. return $items;
  244. }
  245. /**
  246. * Implements hook_menu_alter().
  247. */
  248. function comment_menu_alter(&$items) {
  249. // Add comments to the description for admin/content.
  250. $items['admin/content']['description'] = 'Administer content and comments.';
  251. // Adjust the Field UI tabs on admin/structure/types/manage/[node-type].
  252. // See comment_entity_bundle_info().
  253. $items['admin/structure/types/manage/%/comment/fields']['title'] = 'Comment fields';
  254. $items['admin/structure/types/manage/%/comment/fields']['weight'] = 3;
  255. $items['admin/structure/types/manage/%/comment/display']['title'] = 'Comment display';
  256. $items['admin/structure/types/manage/%/comment/display']['weight'] = 4;
  257. }
  258. /**
  259. * Returns a menu title which includes the number of unapproved comments.
  260. */
  261. function comment_count_unpublished() {
  262. $count = db_query('SELECT COUNT(cid) FROM {comment} WHERE status = :status', array(
  263. ':status' => COMMENT_NOT_PUBLISHED,
  264. ))->fetchField();
  265. return t('Unapproved comments (@count)', array('@count' => $count));
  266. }
  267. /**
  268. * Implements hook_node_type_insert().
  269. *
  270. * Creates a comment body field for a node type created while the Comment module
  271. * is enabled. For node types created before the Comment module is enabled,
  272. * hook_modules_enabled() serves to create the body fields.
  273. *
  274. * @see comment_modules_enabled()
  275. */
  276. function comment_node_type_insert($info) {
  277. _comment_body_field_create($info);
  278. }
  279. /**
  280. * Implements hook_node_type_update().
  281. */
  282. function comment_node_type_update($info) {
  283. if (!empty($info->old_type) && $info->type != $info->old_type) {
  284. entity_invoke_bundle_hook('rename', 'comment', 'comment_node_' . $info->old_type, 'comment_node_' . $info->type);
  285. }
  286. }
  287. /**
  288. * Implements hook_node_type_delete().
  289. */
  290. function comment_node_type_delete($info) {
  291. entity_invoke_bundle_hook('delete', 'comment', 'comment_node_' . $info->type);
  292. $settings = array(
  293. 'comment',
  294. 'comment_default_mode',
  295. 'comment_default_per_page',
  296. 'comment_anonymous',
  297. 'comment_subject_field',
  298. 'comment_preview',
  299. 'comment_form_location',
  300. );
  301. foreach ($settings as $setting) {
  302. variable_del($setting . '_' . $info->type);
  303. }
  304. }
  305. /**
  306. * Creates a comment_body field instance for a given node type.
  307. *
  308. * @param $info
  309. * An object representing the content type. The only property that is
  310. * currently used is $info->type, which is the machine name of the content
  311. * type for which the body field (instance) is to be created.
  312. */
  313. function _comment_body_field_create($info) {
  314. // Create the field if needed.
  315. if (!field_read_field('comment_body', array('include_inactive' => TRUE))) {
  316. $field = array(
  317. 'field_name' => 'comment_body',
  318. 'type' => 'text_long',
  319. 'entity_types' => array('comment'),
  320. );
  321. field_create_field($field);
  322. }
  323. // Create the instance if needed.
  324. if (!field_read_instance('comment', 'comment_body', 'comment_node_' . $info->type, array('include_inactive' => TRUE))) {
  325. entity_invoke_bundle_hook('create', 'comment', 'comment_node_' . $info->type);
  326. // Attaches the body field by default.
  327. $instance = array(
  328. 'field_name' => 'comment_body',
  329. 'label' => 'Comment',
  330. 'entity_type' => 'comment',
  331. 'bundle' => 'comment_node_' . $info->type,
  332. 'settings' => array('text_processing' => 1),
  333. 'required' => TRUE,
  334. );
  335. field_create_instance($instance);
  336. // Assign widget settings for the 'default' form mode.
  337. entity_get_form_display('comment', 'comment_node_' . $info->type, 'default')
  338. ->setComponent('comment_body', array(
  339. 'type' => 'text_textarea',
  340. ))
  341. ->save();
  342. // Assign display settings for the 'default' view mode.
  343. entity_get_display('comment', 'comment_node_' . $info->type, 'default')
  344. ->setComponent('comment_body', array(
  345. 'label' => 'hidden',
  346. 'type' => 'text_default',
  347. 'weight' => 0,
  348. ))
  349. ->save();
  350. }
  351. }
  352. /**
  353. * Implements hook_permission().
  354. */
  355. function comment_permission() {
  356. return array(
  357. 'administer comments' => array(
  358. 'title' => t('Administer comments and comment settings'),
  359. ),
  360. 'access comments' => array(
  361. 'title' => t('View comments'),
  362. ),
  363. 'post comments' => array(
  364. 'title' => t('Post comments'),
  365. ),
  366. 'skip comment approval' => array(
  367. 'title' => t('Skip comment approval'),
  368. ),
  369. 'edit own comments' => array(
  370. 'title' => t('Edit own comments'),
  371. ),
  372. );
  373. }
  374. /**
  375. * Redirects comment links to the correct page depending on comment settings.
  376. *
  377. * Since comments are paged there is no way to guarantee which page a comment
  378. * appears on. Comment paging and threading settings may be changed at any time.
  379. * With threaded comments, an individual comment may move between pages as
  380. * comments can be added either before or after it in the overall discussion.
  381. * Therefore we use a central routing function for comment links, which
  382. * calculates the page number based on current comment settings and returns
  383. * the full comment view with the pager set dynamically.
  384. *
  385. * @param \Drupal\comment\Plugin\Core\Entity\Comment $comment
  386. * A comment entity.
  387. *
  388. * @return
  389. * The comment listing set to the page on which the comment appears.
  390. */
  391. function comment_permalink(Comment $comment) {
  392. if ($node = $comment->nid->entity) {
  393. // Find the current display page for this comment.
  394. $page = comment_get_display_page($comment->id(), $node->type);
  395. // @todo: Cleaner sub request handling.
  396. $request = drupal_container()->get('request');
  397. $subrequest = Request::create('/node/' . $node->nid, 'GET', $request->query->all(), $request->cookies->all(), array(), $request->server->all());
  398. $subrequest->query->set('page', $page);
  399. // @todo: Convert the pager to use the request object.
  400. $_GET['page'] = $page;
  401. return drupal_container()->get('http_kernel')->handle($subrequest, HttpKernelInterface::SUB_REQUEST);
  402. }
  403. throw new NotFoundHttpException();
  404. }
  405. /**
  406. * Finds the most recent comments that are available to the current user.
  407. *
  408. * @param integer $number
  409. * (optional) The maximum number of comments to find. Defaults to 10.
  410. *
  411. * @return
  412. * An array of comment objects or an empty array if there are no recent
  413. * comments visible to the current user.
  414. */
  415. function comment_get_recent($number = 10) {
  416. $query = db_select('comment', 'c');
  417. $query->innerJoin('node_field_data', 'n', 'n.nid = c.nid');
  418. $query->addTag('node_access');
  419. $query->addMetaData('base_table', 'comment');
  420. $comments = $query
  421. ->fields('c')
  422. ->condition('c.status', COMMENT_PUBLISHED)
  423. ->condition('n.status', NODE_PUBLISHED)
  424. // @todo This should be actually filtering on the desired node status field
  425. // language and just fall back to the default language.
  426. ->condition('n.default_langcode', 1)
  427. ->orderBy('c.created', 'DESC')
  428. // Additionally order by cid to ensure that comments with the same timestamp
  429. // are returned in the exact order posted.
  430. ->orderBy('c.cid', 'DESC')
  431. ->range(0, $number)
  432. ->execute()
  433. ->fetchAll();
  434. return $comments ? $comments : array();
  435. }
  436. /**
  437. * Calculates the page number for the first new comment.
  438. *
  439. * @param $num_comments
  440. * Number of comments.
  441. * @param $new_replies
  442. * Number of new replies.
  443. * @param \Drupal\Core\Entity\EntityInterface $node
  444. * The first new comment node.
  445. *
  446. * @return
  447. * "page=X" if the page number is greater than zero; empty string otherwise.
  448. */
  449. function comment_new_page_count($num_comments, $new_replies, EntityInterface $node) {
  450. $mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED);
  451. $comments_per_page = variable_get('comment_default_per_page_' . $node->type, 50);
  452. $pagenum = NULL;
  453. $flat = $mode == COMMENT_MODE_FLAT ? TRUE : FALSE;
  454. if ($num_comments <= $comments_per_page) {
  455. // Only one page of comments.
  456. $pageno = 0;
  457. }
  458. elseif ($flat) {
  459. // Flat comments.
  460. $count = $num_comments - $new_replies;
  461. $pageno = $count / $comments_per_page;
  462. }
  463. else {
  464. // Threaded comments: we build a query with a subquery to find the first
  465. // thread with a new comment.
  466. // 1. Find all the threads with a new comment.
  467. $unread_threads_query = db_select('comment')
  468. ->fields('comment', array('thread'))
  469. ->condition('nid', $node->nid)
  470. ->condition('status', COMMENT_PUBLISHED)
  471. ->orderBy('created', 'DESC')
  472. ->orderBy('cid', 'DESC')
  473. ->range(0, $new_replies);
  474. // 2. Find the first thread.
  475. $first_thread = db_select($unread_threads_query, 'thread')
  476. ->fields('thread', array('thread'))
  477. ->orderBy('SUBSTRING(thread, 1, (LENGTH(thread) - 1))')
  478. ->range(0, 1)
  479. ->execute()
  480. ->fetchField();
  481. // Remove the final '/'.
  482. $first_thread = substr($first_thread, 0, -1);
  483. // Find the number of the first comment of the first unread thread.
  484. $count = db_query('SELECT COUNT(*) FROM {comment} WHERE nid = :nid AND status = :status AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < :thread', array(
  485. ':status' => COMMENT_PUBLISHED,
  486. ':nid' => $node->nid,
  487. ':thread' => $first_thread,
  488. ))->fetchField();
  489. $pageno = $count / $comments_per_page;
  490. }
  491. if ($pageno >= 1) {
  492. $pagenum = array('page' => intval($pageno));
  493. }
  494. return $pagenum;
  495. }
  496. /**
  497. * Returns HTML for a list of recent comments.
  498. *
  499. * @ingroup themeable
  500. */
  501. function theme_comment_block($variables) {
  502. $items = array();
  503. $number = $variables['number'];
  504. foreach (comment_get_recent($number) as $comment) {
  505. $items[] = l($comment->subject, 'comment/' . $comment->cid, array('fragment' => 'comment-' . $comment->cid)) . '&nbsp;<span>' . t('@time ago', array('@time' => format_interval(REQUEST_TIME - $comment->changed))) . '</span>';
  506. }
  507. if ($items) {
  508. return theme('item_list', array('items' => $items));
  509. }
  510. else {
  511. return t('No comments available.');
  512. }
  513. }
  514. /**
  515. * Implements hook_node_view().
  516. */
  517. function comment_node_view(EntityInterface $node, EntityDisplay $display, $view_mode) {
  518. $links = array();
  519. if ($node->comment != COMMENT_NODE_HIDDEN) {
  520. if ($view_mode == 'rss') {
  521. // Add a comments RSS element which is a URL to the comments of this node.
  522. $node->rss_elements[] = array(
  523. 'key' => 'comments',
  524. 'value' => url('node/' . $node->nid, array('fragment' => 'comments', 'absolute' => TRUE))
  525. );
  526. }
  527. elseif ($view_mode == 'teaser') {
  528. // Teaser view: display the number of comments that have been posted,
  529. // or a link to add new comments if the user has permission, the node
  530. // is open to new comments, and there currently are none.
  531. if (user_access('access comments')) {
  532. if (!empty($node->comment_count)) {
  533. $links['comment-comments'] = array(
  534. 'title' => format_plural($node->comment_count, '1 comment', '@count comments'),
  535. 'href' => "node/$node->nid",
  536. 'attributes' => array('title' => t('Jump to the first comment of this posting.')),
  537. 'fragment' => 'comments',
  538. 'html' => TRUE,
  539. );
  540. // Show a link to the first new comment.
  541. if ($new = comment_num_new($node->nid)) {
  542. $links['comment-new-comments'] = array(
  543. 'title' => format_plural($new, '1 new comment', '@count new comments'),
  544. 'href' => "node/$node->nid",
  545. 'query' => comment_new_page_count($node->comment_count, $new, $node),
  546. 'attributes' => array('title' => t('Jump to the first new comment of this posting.')),
  547. 'fragment' => 'new',
  548. 'html' => TRUE,
  549. );
  550. }
  551. }
  552. }
  553. if ($node->comment == COMMENT_NODE_OPEN) {
  554. $comment_form_location = variable_get('comment_form_location_' . $node->type, COMMENT_FORM_BELOW);
  555. if (user_access('post comments')) {
  556. $links['comment-add'] = array(
  557. 'title' => t('Add new comment'),
  558. 'href' => "node/$node->nid",
  559. 'attributes' => array('title' => t('Add a new comment to this page.')),
  560. 'fragment' => 'comment-form',
  561. );
  562. if ($comment_form_location == COMMENT_FORM_SEPARATE_PAGE) {
  563. $links['comment-add']['href'] = "comment/reply/$node->nid";
  564. }
  565. }
  566. else {
  567. $links['comment-forbidden'] = array(
  568. 'title' => theme('comment_post_forbidden', array('node' => $node)),
  569. 'html' => TRUE,
  570. );
  571. }
  572. }
  573. }
  574. elseif ($view_mode != 'search_index' && $view_mode != 'search_result') {
  575. // Node in other view modes: add a "post comment" link if the user is
  576. // allowed to post comments and if this node is allowing new comments.
  577. // But we don't want this link if we're building the node for search
  578. // indexing or constructing a search result excerpt.
  579. if ($node->comment == COMMENT_NODE_OPEN) {
  580. $comment_form_location = variable_get('comment_form_location_' . $node->type, COMMENT_FORM_BELOW);
  581. if (user_access('post comments')) {
  582. // Show the "post comment" link if the form is on another page, or
  583. // if there are existing comments that the link will skip past.
  584. if ($comment_form_location == COMMENT_FORM_SEPARATE_PAGE || (!empty($node->comment_count) && user_access('access comments'))) {
  585. $links['comment-add'] = array(
  586. 'title' => t('Add new comment'),
  587. 'attributes' => array('title' => t('Share your thoughts and opinions related to this posting.')),
  588. 'href' => "node/$node->nid",
  589. 'fragment' => 'comment-form',
  590. );
  591. if ($comment_form_location == COMMENT_FORM_SEPARATE_PAGE) {
  592. $links['comment-add']['href'] = "comment/reply/$node->nid";
  593. }
  594. }
  595. }
  596. else {
  597. $links['comment-forbidden'] = array(
  598. 'title' => theme('comment_post_forbidden', array('node' => $node)),
  599. 'html' => TRUE,
  600. );
  601. }
  602. }
  603. }
  604. $node->content['links']['comment'] = array(
  605. '#theme' => 'links__node__comment',
  606. '#links' => $links,
  607. '#attributes' => array('class' => array('links', 'inline')),
  608. );
  609. // Only append comments when we are building a node on its own node detail
  610. // page. We compare $node and $page_node to ensure that comments are not
  611. // appended to other nodes shown on the page, for example a node_reference
  612. // displayed in 'full' view mode within another node.
  613. if ($node->comment && $view_mode == 'full' && node_is_page($node) && empty($node->in_preview)) {
  614. $node->content['comments'] = comment_node_page_additions($node);
  615. }
  616. }
  617. }
  618. /**
  619. * Builds the comment-related elements for node detail pages.
  620. *
  621. * @param \Drupal\Core\Entity\EntityInterface $node
  622. * The node entity for which to build the comment-related elements.
  623. *
  624. * @return
  625. * A renderable array representing the comment-related page elements for the
  626. * node.
  627. */
  628. function comment_node_page_additions(EntityInterface $node) {
  629. $additions = array();
  630. // Only attempt to render comments if the node has visible comments.
  631. // Unpublished comments are not included in $node->comment_count, so show
  632. // comments unconditionally if the user is an administrator.
  633. if (($node->comment_count && user_access('access comments')) || user_access('administer comments')) {
  634. $mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED);
  635. $comments_per_page = variable_get('comment_default_per_page_' . $node->type, 50);
  636. if ($cids = comment_get_thread($node, $mode, $comments_per_page)) {
  637. $comments = comment_load_multiple($cids);
  638. comment_prepare_thread($comments);
  639. $build = comment_view_multiple($comments);
  640. $build['pager']['#theme'] = 'pager';
  641. $additions['comments'] = $build;
  642. }
  643. }
  644. // Append comment form if needed.
  645. if (user_access('post comments') && $node->comment == COMMENT_NODE_OPEN && (variable_get('comment_form_location_' . $node->type, COMMENT_FORM_BELOW) == COMMENT_FORM_BELOW)) {
  646. $additions['comment_form'] = comment_add($node);
  647. }
  648. if ($additions) {
  649. $additions += array(
  650. '#theme' => 'comment_wrapper__node_' . $node->type,
  651. '#node' => $node,
  652. 'comments' => array(),
  653. 'comment_form' => array(),
  654. );
  655. }
  656. return $additions;
  657. }
  658. /**
  659. * Returns a rendered form to comment the given node.
  660. *
  661. * @param \Drupal\Core\Entity\EntityInterface $node
  662. * The node entity to be commented.
  663. * @param int $pid
  664. * (optional) Some comments are replies to other comments. In those cases,
  665. * $pid is the parent comment's comment ID. Defaults to NULL.
  666. *
  667. * @return array
  668. * The renderable array for the comment addition form.
  669. */
  670. function comment_add(EntityInterface $node, $pid = NULL) {
  671. $values = array('nid' => $node->nid, 'pid' => $pid, 'node_type' => 'comment_node_' . $node->type);
  672. $comment = entity_create('comment', $values);
  673. return entity_get_form($comment);
  674. }
  675. /**
  676. * Retrieves comments for a thread.
  677. *
  678. * @param \Drupal\Core\Entity\EntityInterface $node
  679. * The node whose comment(s) needs rendering.
  680. * @param $mode
  681. * The comment display mode; COMMENT_MODE_FLAT or COMMENT_MODE_THREADED.
  682. * @param $comments_per_page
  683. * The amount of comments to display per page.
  684. *
  685. * @return
  686. * An array of the IDs of the comment to be displayed.
  687. *
  688. * To display threaded comments in the correct order we keep a 'thread' field
  689. * and order by that value. This field keeps this data in
  690. * a way which is easy to update and convenient to use.
  691. *
  692. * A "thread" value starts at "1". If we add a child (A) to this comment,
  693. * we assign it a "thread" = "1.1". A child of (A) will have "1.1.1". Next
  694. * brother of (A) will get "1.2". Next brother of the parent of (A) will get
  695. * "2" and so on.
  696. *
  697. * First of all note that the thread field stores the depth of the comment:
  698. * depth 0 will be "X", depth 1 "X.X", depth 2 "X.X.X", etc.
  699. *
  700. * Now to get the ordering right, consider this example:
  701. *
  702. * 1
  703. * 1.1
  704. * 1.1.1
  705. * 1.2
  706. * 2
  707. *
  708. * If we "ORDER BY thread ASC" we get the above result, and this is the
  709. * natural order sorted by time. However, if we "ORDER BY thread DESC"
  710. * we get:
  711. *
  712. * 2
  713. * 1.2
  714. * 1.1.1
  715. * 1.1
  716. * 1
  717. *
  718. * Clearly, this is not a natural way to see a thread, and users will get
  719. * confused. The natural order to show a thread by time desc would be:
  720. *
  721. * 2
  722. * 1
  723. * 1.2
  724. * 1.1
  725. * 1.1.1
  726. *
  727. * which is what we already did before the standard pager patch. To achieve
  728. * this we simply add a "/" at the end of each "thread" value. This way, the
  729. * thread fields will look like this:
  730. *
  731. * 1/
  732. * 1.1/
  733. * 1.1.1/
  734. * 1.2/
  735. * 2/
  736. *
  737. * we add "/" since this char is, in ASCII, higher than every number, so if
  738. * now we "ORDER BY thread DESC" we get the correct order. However this would
  739. * spoil the reverse ordering, "ORDER BY thread ASC" -- here, we do not need
  740. * to consider the trailing "/" so we use a substring only.
  741. */
  742. function comment_get_thread(EntityInterface $node, $mode, $comments_per_page) {
  743. $query = db_select('comment', 'c')
  744. ->extend('Drupal\Core\Database\Query\PagerSelectExtender');
  745. $query->addField('c', 'cid');
  746. $query
  747. ->condition('c.nid', $node->nid)
  748. ->addTag('node_access')
  749. ->addTag('comment_filter')
  750. ->addMetaData('base_table', 'comment')
  751. ->addMetaData('node', $node)
  752. ->limit($comments_per_page);
  753. $count_query = db_select('comment', 'c');
  754. $count_query->addExpression('COUNT(*)');
  755. $count_query
  756. ->condition('c.nid', $node->nid)
  757. ->addTag('node_access')
  758. ->addTag('comment_filter')
  759. ->addMetaData('base_table', 'comment')
  760. ->addMetaData('node', $node);
  761. if (!user_access('administer comments')) {
  762. $query->condition('c.status', COMMENT_PUBLISHED);
  763. $count_query->condition('c.status', COMMENT_PUBLISHED);
  764. }
  765. if ($mode === COMMENT_MODE_FLAT) {
  766. $query->orderBy('c.cid', 'ASC');
  767. }
  768. else {
  769. // See comment above. Analysis reveals that this doesn't cost too
  770. // much. It scales much much better than having the whole comment
  771. // structure.
  772. $query->addExpression('SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))', 'torder');
  773. $query->orderBy('torder', 'ASC');
  774. }
  775. $query->setCountQuery($count_query);
  776. $cids = $query->execute()->fetchCol();
  777. return $cids;
  778. }
  779. /**
  780. * Calculates the indentation level of each comment in a comment thread.
  781. *
  782. * This function loops over an array representing a comment thread. For each
  783. * comment, the function calculates the indentation level and saves it in the
  784. * 'divs' property of the comment object.
  785. *
  786. * @param array $comments
  787. * An array of comment objects, keyed by comment ID.
  788. */
  789. function comment_prepare_thread(&$comments) {
  790. // A flag stating if we are still searching for first new comment on the thread.
  791. $first_new = TRUE;
  792. // A counter that helps track how indented we are.
  793. $divs = 0;
  794. foreach ($comments as $key => $comment) {
  795. if ($first_new && $comment->new->value != MARK_READ) {
  796. // Assign the anchor only for the first new comment. This avoids duplicate
  797. // id attributes on a page.
  798. $first_new = FALSE;
  799. $comment->first_new = TRUE;
  800. }
  801. // The $divs element instructs #prefix whether to add an indent div or
  802. // close existing divs (a negative value).
  803. $comment->depth = count(explode('.', $comment->thread->value)) - 1;
  804. if ($comment->depth > $divs) {
  805. $comment->divs = 1;
  806. $divs++;
  807. }
  808. else {
  809. $comment->divs = $comment->depth - $divs;
  810. while ($comment->depth < $divs) {
  811. $divs--;
  812. }
  813. }
  814. $comments[$key] = $comment;
  815. }
  816. // The final comment must close up some hanging divs
  817. $comments[$key]->divs_final = $divs;
  818. }
  819. /**
  820. * Generates an array for rendering a comment.
  821. *
  822. * @param Drupal\comment\Comment $comment
  823. * The comment object.
  824. * @param $view_mode
  825. * View mode, e.g. 'full', 'teaser'...
  826. * @param $langcode
  827. * (optional) A language code to use for rendering. Defaults to the global
  828. * content language of the current request.
  829. *
  830. * @return
  831. * An array as expected by drupal_render().
  832. */
  833. function comment_view(Comment $comment, $view_mode = 'full', $langcode = NULL) {
  834. return entity_view($comment, $view_mode, $langcode);
  835. }
  836. /**
  837. * Adds reply, edit, delete, etc. links, depending on user permissions.
  838. *
  839. * @param Drupal\comment\Comment $comment
  840. * The comment object.
  841. * @param \Drupal\Core\Entity\EntityInterface $node
  842. * The node the comment is attached to.
  843. *
  844. * @return
  845. * A structured array of links.
  846. */
  847. function comment_links(Comment $comment, EntityInterface $node) {
  848. $links = array();
  849. if ($node->comment == COMMENT_NODE_OPEN) {
  850. if ($comment->access('delete')) {
  851. $links['comment-delete'] = array(
  852. 'title' => t('delete'),
  853. 'href' => "comment/{$comment->id()}/delete",
  854. 'html' => TRUE,
  855. );
  856. }
  857. if ($comment->access('update')) {
  858. $links['comment-edit'] = array(
  859. 'title' => t('edit'),
  860. 'href' => "comment/{$comment->id()}/edit",
  861. 'html' => TRUE,
  862. );
  863. }
  864. if ($comment->access('create')) {
  865. $links['comment-reply'] = array(
  866. 'title' => t('reply'),
  867. 'href' => "comment/reply/{$comment->nid->target_id}/{$comment->id()}",
  868. 'html' => TRUE,
  869. );
  870. }
  871. if ($comment->status->value == COMMENT_NOT_PUBLISHED && $comment->access('approve')) {
  872. $links['comment-approve'] = array(
  873. 'title' => t('approve'),
  874. 'href' => "comment/{$comment->id()}/approve",
  875. 'html' => TRUE,
  876. 'query' => array('token' => drupal_get_token("comment/{$comment->id()}/approve")),
  877. );
  878. }
  879. if (empty($links)) {
  880. $links['comment-forbidden']['title'] = theme('comment_post_forbidden', array('node' => $node));
  881. $links['comment-forbidden']['html'] = TRUE;
  882. }
  883. }
  884. // Add translations link for translation-enabled comment bundles.
  885. if (module_exists('translation_entity') && translation_entity_translate_access($comment)) {
  886. $links['comment-translations'] = array(
  887. 'title' => t('translate'),
  888. 'href' => 'comment/' . $comment->id() . '/translations',
  889. 'html' => TRUE,
  890. );
  891. }
  892. return $links;
  893. }
  894. /**
  895. * Constructs render array from an array of loaded comments.
  896. *
  897. * @param $comments
  898. * An array of comments as returned by comment_load_multiple().
  899. * @param $view_mode
  900. * View mode, e.g. 'full', 'teaser'...
  901. * @param $langcode
  902. * A string indicating the language field values are to be shown in. If no
  903. * language is provided the current content language is used.
  904. *
  905. * @return
  906. * An array in the format expected by drupal_render().
  907. *
  908. * @see drupal_render()
  909. */
  910. function comment_view_multiple($comments, $view_mode = 'full', $langcode = NULL) {
  911. return entity_view_multiple($comments, $view_mode, $langcode);
  912. }
  913. /**
  914. * Implements hook_form_FORM_ID_alter().
  915. */
  916. function comment_form_node_type_form_alter(&$form, $form_state) {
  917. if (isset($form['type'])) {
  918. $form['comment'] = array(
  919. '#type' => 'details',
  920. '#title' => t('Comment settings'),
  921. '#collapsed' => TRUE,
  922. '#group' => 'additional_settings',
  923. '#attributes' => array(
  924. 'class' => array('comment-node-type-settings-form'),
  925. ),
  926. '#attached' => array(
  927. 'library' => array(array('comment', 'drupal.comment')),
  928. ),
  929. );
  930. // Unlike coment_form_node_form_alter(), all of these settings are applied
  931. // as defaults to all new nodes. Therefore, it would be wrong to use #states
  932. // to hide the other settings based on the primary comment setting.
  933. $form['comment']['comment'] = array(
  934. '#type' => 'select',
  935. '#title' => t('Default comment setting for new content'),
  936. '#default_value' => variable_get('comment_' . $form['#node_type']->type, COMMENT_NODE_OPEN),
  937. '#options' => array(
  938. COMMENT_NODE_OPEN => t('Open'),
  939. COMMENT_NODE_CLOSED => t('Closed'),
  940. COMMENT_NODE_HIDDEN => t('Hidden'),
  941. ),
  942. );
  943. $form['comment']['comment_default_mode'] = array(
  944. '#type' => 'checkbox',
  945. '#title' => t('Threading'),
  946. '#default_value' => variable_get('comment_default_mode_' . $form['#node_type']->type, COMMENT_MODE_THREADED),
  947. '#description' => t('Show comment replies in a threaded list.'),
  948. );
  949. $form['comment']['comment_default_per_page'] = array(
  950. '#type' => 'select',
  951. '#title' => t('Comments per page'),
  952. '#default_value' => variable_get('comment_default_per_page_' . $form['#node_type']->type, 50),
  953. '#options' => _comment_per_page(),
  954. );
  955. $form['comment']['comment_anonymous'] = array(
  956. '#type' => 'select',
  957. '#title' => t('Anonymous commenting'),
  958. '#default_value' => variable_get('comment_anonymous_' . $form['#node_type']->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT),
  959. '#options' => array(
  960. COMMENT_ANONYMOUS_MAYNOT_CONTACT => t('Anonymous posters may not enter their contact information'),
  961. COMMENT_ANONYMOUS_MAY_CONTACT => t('Anonymous posters may leave their contact information'),
  962. COMMENT_ANONYMOUS_MUST_CONTACT => t('Anonymous posters must leave their contact information'),
  963. ),
  964. '#access' => user_access('post comments', drupal_anonymous_user()),
  965. );
  966. $form['comment']['comment_subject_field'] = array(
  967. '#type' => 'checkbox',
  968. '#title' => t('Allow comment title'),
  969. '#default_value' => variable_get('comment_subject_field_' . $form['#node_type']->type, 1),
  970. );
  971. $form['comment']['comment_form_location'] = array(
  972. '#type' => 'checkbox',
  973. '#title' => t('Show reply form on the same page as comments'),
  974. '#default_value' => variable_get('comment_form_location_' . $form['#node_type']->type, COMMENT_FORM_BELOW),
  975. );
  976. $form['comment']['comment_preview'] = array(
  977. '#type' => 'radios',
  978. '#title' => t('Preview comment'),
  979. '#default_value' => variable_get('comment_preview_' . $form['#node_type']->type, DRUPAL_OPTIONAL),
  980. '#options' => array(
  981. DRUPAL_DISABLED => t('Disabled'),
  982. DRUPAL_OPTIONAL => t('Optional'),
  983. DRUPAL_REQUIRED => t('Required'),
  984. ),
  985. );
  986. // @todo Remove this check once language settings are generalized.
  987. if (module_exists('translation_entity')) {
  988. $comment_form = $form;
  989. $comment_form_state['translation_entity']['key'] = 'language_configuration';
  990. $form['comment'] += translation_entity_enable_widget('comment', 'comment_node_' . $form['#node_type']->type, $comment_form, $comment_form_state);
  991. array_unshift($form['#submit'], 'comment_translation_configuration_element_submit');
  992. }
  993. }
  994. }
  995. /**
  996. * Form submission handler for node_type_form().
  997. *
  998. * This handles the comment translation settings added by
  999. * comment_form_node_type_form_alter().
  1000. *
  1001. * @see comment_form_node_type_form_alter()
  1002. */
  1003. function comment_translation_configuration_element_submit($form, &$form_state) {
  1004. // The comment translation settings form element is embedded into the node
  1005. // type form. Hence we need to provide to the regular submit handler a
  1006. // manipulated form state to make it process comment settings instead of node
  1007. // settings.
  1008. $key = 'language_configuration';
  1009. $comment_form_state = array(
  1010. 'translation_entity' => array('key' => $key),
  1011. 'language' => array($key => array('entity_type' => 'comment', 'bundle' => 'comment_node_' . $form['#node_type']->type)),
  1012. 'values' => array($key => array('translation_entity' => $form_state['values']['translation_entity'])),
  1013. );
  1014. translation_entity_language_configuration_element_submit($form, $comment_form_state);
  1015. }
  1016. /**
  1017. * Implements hook_form_BASE_FORM_ID_alter().
  1018. */
  1019. function comment_form_node_form_alter(&$form, $form_state) {
  1020. $node = $form_state['controller']->getEntity();
  1021. $form['comment_settings'] = array(
  1022. '#type' => 'details',
  1023. '#access' => user_access('administer comments'),
  1024. '#title' => t('Comment settings'),
  1025. '#collapsed' => TRUE,
  1026. '#group' => 'advanced',
  1027. '#attributes' => array(
  1028. 'class' => array('comment-node-settings-form'),
  1029. ),
  1030. '#attached' => array(
  1031. 'library' => array(array('comment', 'drupal.comment')),
  1032. ),
  1033. '#weight' => 30,
  1034. );
  1035. $comment_count = isset($node->nid) ? db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = :nid', array(':nid' => $node->nid))->fetchField() : 0;
  1036. $comment_settings = ($node->comment == COMMENT_NODE_HIDDEN && empty($comment_count)) ? COMMENT_NODE_CLOSED : $node->comment;
  1037. $form['comment_settings']['comment'] = array(
  1038. '#type' => 'radios',
  1039. '#title' => t('Comments'),
  1040. '#title_display' => 'invisible',
  1041. '#parents' => array('comment'),
  1042. '#default_value' => $comment_settings,
  1043. '#options' => array(
  1044. COMMENT_NODE_OPEN => t('Open'),
  1045. COMMENT_NODE_CLOSED => t('Closed'),
  1046. COMMENT_NODE_HIDDEN => t('Hidden'),
  1047. ),
  1048. COMMENT_NODE_OPEN => array(
  1049. '#description' => t('Users with the "Post comments" permission can post comments.'),
  1050. ),
  1051. COMMENT_NODE_CLOSED => array(
  1052. '#description' => t('Users cannot post comments, but existing comments will be displayed.'),
  1053. ),
  1054. COMMENT_NODE_HIDDEN => array(
  1055. '#description' => t('Comments are hidden from view.'),
  1056. ),
  1057. );
  1058. // If the node doesn't have any comments, the "hidden" option makes no
  1059. // sense, so don't even bother presenting it to the user.
  1060. if (empty($comment_count)) {
  1061. $form['comment_settings']['comment'][COMMENT_NODE_HIDDEN]['#access'] = FALSE;
  1062. // Also adjust the description of the "closed" option.
  1063. $form['comment_settings']['comment'][COMMENT_NODE_CLOSED]['#description'] = t('Users cannot post comments.');
  1064. }
  1065. }
  1066. /**
  1067. * Implements hook_node_load().
  1068. */
  1069. function comment_node_load($nodes, $types) {
  1070. $comments_enabled = array();
  1071. // Check if comments are enabled for each node. If comments are disabled,
  1072. // assign values without hitting the database.
  1073. foreach ($nodes as $node) {
  1074. // Store whether comments are enabled for this node.
  1075. if ($node->comment != COMMENT_NODE_HIDDEN) {
  1076. $comments_enabled[] = $node->nid;
  1077. }
  1078. else {
  1079. $node->cid = 0;
  1080. $node->last_comment_timestamp = $node->created;
  1081. $node->last_comment_name = '';
  1082. $node->last_comment_uid = $node->uid;
  1083. $node->comment_count = 0;
  1084. }
  1085. }
  1086. // For nodes with comments enabled, fetch information from the database.
  1087. if (!empty($comments_enabled)) {
  1088. $result = db_query('SELECT nid, cid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count FROM {node_comment_statistics} WHERE nid IN (:comments_enabled)', array(':comments_enabled' => $comments_enabled));
  1089. foreach ($result as $record) {
  1090. $nodes[$record->nid]->cid = $record->cid;
  1091. $nodes[$record->nid]->last_comment_timestamp = $record->last_comment_timestamp;
  1092. $nodes[$record->nid]->last_comment_name = $record->last_comment_name;
  1093. $nodes[$record->nid]->last_comment_uid = $record->last_comment_uid;
  1094. $nodes[$record->nid]->comment_count = $record->comment_count;
  1095. }
  1096. }
  1097. }
  1098. /**
  1099. * Implements hook_node_prepare().
  1100. */
  1101. function comment_node_prepare(EntityInterface $node) {
  1102. if (!isset($node->comment)) {
  1103. $node->comment = variable_get("comment_$node->type", COMMENT_NODE_OPEN);
  1104. }
  1105. }
  1106. /**
  1107. * Implements hook_node_insert().
  1108. */
  1109. function comment_node_insert(EntityInterface $node) {
  1110. // Allow bulk updates and inserts to temporarily disable the
  1111. // maintenance of the {node_comment_statistics} table.
  1112. if (variable_get('comment_maintain_node_statistics', TRUE)) {
  1113. db_insert('node_comment_statistics')
  1114. ->fields(array(
  1115. 'nid' => $node->nid,
  1116. 'cid' => 0,
  1117. 'last_comment_timestamp' => $node->changed,
  1118. 'last_comment_name' => NULL,
  1119. 'last_comment_uid' => $node->uid,
  1120. 'comment_count' => 0,
  1121. ))
  1122. ->execute();
  1123. }
  1124. }
  1125. /**
  1126. * Implements hook_node_predelete().
  1127. */
  1128. function comment_node_predelete(EntityInterface $node) {
  1129. $cids = db_query('SELECT cid FROM {comment} WHERE nid = :nid', array(':nid' => $node->nid))->fetchCol();
  1130. entity_delete_multiple('comment', $cids);
  1131. db_delete('node_comment_statistics')
  1132. ->condition('nid', $node->nid)
  1133. ->execute();
  1134. }
  1135. /**
  1136. * Implements hook_node_update_index().
  1137. */
  1138. function comment_node_update_index(EntityInterface $node, $langcode) {
  1139. $index_comments = &drupal_static(__FUNCTION__);
  1140. if ($index_comments === NULL) {
  1141. // Find and save roles that can 'access comments' or 'search content'.
  1142. $perms = array('access comments' => array(), 'search content' => array());
  1143. $result = db_query("SELECT rid, permission FROM {role_permission} WHERE permission IN ('access comments', 'search content')");
  1144. foreach ($result as $record) {
  1145. $perms[$record->permission][$record->rid] = $record->rid;
  1146. }
  1147. // Prevent indexing of comments if there are any roles that can search but
  1148. // not view comments.
  1149. $index_comments = TRUE;
  1150. foreach ($perms['search content'] as $rid) {
  1151. if (!isset($perms['access comments'][$rid]) && (($rid == DRUPAL_AUTHENTICATED_RID || $rid == DRUPAL_ANONYMOUS_RID) || !isset($perms['access comments'][DRUPAL_AUTHENTICATED_RID]))) {
  1152. $index_comments = FALSE;
  1153. break;
  1154. }
  1155. }
  1156. }
  1157. if ($index_comments) {
  1158. $mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED);
  1159. $comments_per_page = variable_get('comment_default_per_page_' . $node->type, 50);
  1160. if ($node->comment && $cids = comment_get_thread($node, $mode, $comments_per_page)) {
  1161. $comments = comment_load_multiple($cids);
  1162. comment_prepare_thread($comments);
  1163. $build = comment_view_multiple($comments, $langcode);
  1164. return drupal_render($build);
  1165. }
  1166. }
  1167. return '';
  1168. }
  1169. /**
  1170. * Implements hook_update_index().
  1171. */
  1172. function comment_update_index() {
  1173. // Store the maximum possible comments per thread (used for ranking by reply count)
  1174. Drupal::state()->set('comment.node_comment_statistics_scale', 1.0 / max(1, db_query('SELECT MAX(comment_count) FROM {node_comment_statistics}')->fetchField()));
  1175. }
  1176. /**
  1177. * Implements hook_node_search_result().
  1178. *
  1179. * Formats a comment count string and returns it, for display with search
  1180. * results.
  1181. */
  1182. function comment_node_search_result(EntityInterface $node) {
  1183. // Do not make a string if comments are hidden.
  1184. if (user_access('access comments') && $node->comment != COMMENT_NODE_HIDDEN) {
  1185. $comments = db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = :nid', array('nid' => $node->nid))->fetchField();
  1186. // Do not make a string if comments are closed and there are currently
  1187. // zero comments.
  1188. if ($node->comment != COMMENT_NODE_CLOSED || $comments > 0) {
  1189. return array('comment' => format_plural($comments, '1 comment', '@count comments'));
  1190. }
  1191. }
  1192. }
  1193. /**
  1194. * Implements hook_user_cancel().
  1195. */
  1196. function comment_user_cancel($edit, $account, $method) {
  1197. switch ($method) {
  1198. case 'user_cancel_block_unpublish':
  1199. $comments = entity_load_multiple_by_properties('comment', array('uid' => $account->uid));
  1200. foreach ($comments as $comment) {
  1201. $comment->status->value = 0;
  1202. $comment->save();
  1203. }
  1204. break;
  1205. case 'user_cancel_reassign':
  1206. $comments = entity_load_multiple_by_properties('comment', array('uid' => $account->uid));
  1207. foreach ($comments as $comment) {
  1208. $comment->uid->target_id = 0;
  1209. $comment->save();
  1210. }
  1211. break;
  1212. }
  1213. }
  1214. /**
  1215. * Implements hook_user_predelete().
  1216. */
  1217. function comment_user_predelete($account) {
  1218. $cids = db_query('SELECT c.cid FROM {comment} c WHERE uid = :uid', array(':uid' => $account->uid))->fetchCol();
  1219. entity_delete_multiple('comment', $cids);
  1220. }
  1221. /**
  1222. * Loads comment entities from the database.
  1223. *
  1224. * @param array $cids
  1225. * (optional) An array of entity IDs. If omitted, all entities are loaded.
  1226. * @param bool $reset
  1227. * (optional) Whether to reset the internal static entity cache.
  1228. *
  1229. * @return array
  1230. * An array of comment objects, indexed by comment ID.
  1231. *
  1232. * @see entity_load()
  1233. * @see Drupal\Core\Entity\Query\QueryInterface
  1234. */
  1235. function comment_load_multiple(array $cids = NULL, $reset = FALSE) {
  1236. return entity_load_multiple('comment', $cids, $reset);
  1237. }
  1238. /**
  1239. * Loads the entire comment by comment ID.
  1240. *
  1241. * @param int $cid
  1242. * The ID of the comment to be loaded.
  1243. * @param bool $reset
  1244. * (optional) Whether to reset the internal static entity cache.
  1245. *
  1246. * @return
  1247. * The comment object.
  1248. */
  1249. function comment_load($cid, $reset = FALSE) {
  1250. return entity_load('comment', $cid, $reset);
  1251. }
  1252. /**
  1253. * Gets the number of new comments for the current user and the specified node.
  1254. *
  1255. * @param $nid
  1256. * Node ID to count comments for.
  1257. * @param $timestamp
  1258. * Time to count from (defaults to time of last user access
  1259. * to node).
  1260. *
  1261. * @return
  1262. * The number of new comments or FALSE if the user is not logged in.
  1263. */
  1264. function comment_num_new($nid, $timestamp = 0) {
  1265. global $user;
  1266. if ($user->uid && module_exists('history')) {
  1267. // Retrieve the timestamp at which the current user last viewed this node.
  1268. if (!$timestamp) {
  1269. $timestamp = history_read($nid);
  1270. }
  1271. $timestamp = ($timestamp > HISTORY_READ_LIMIT ? $timestamp : HISTORY_READ_LIMIT);
  1272. // Use the timestamp to retrieve the number of new comments.
  1273. return db_query('SELECT COUNT(cid) FROM {comment} WHERE nid = :nid AND created > :timestamp AND status = :status', array(
  1274. ':nid' => $nid,
  1275. ':timestamp' => $timestamp,
  1276. ':status' => COMMENT_PUBLISHED,
  1277. ))->fetchField();
  1278. }
  1279. else {
  1280. return FALSE;
  1281. }
  1282. }
  1283. /**
  1284. * Gets the display ordinal for a comment, starting from 0.
  1285. *
  1286. * Count the number of comments which appear before the comment we want to
  1287. * display, taking into account display settings and threading.
  1288. *
  1289. * @param $cid
  1290. * The comment ID.
  1291. * @param $node_type
  1292. * The node type of the comment's parent.
  1293. *
  1294. * @return
  1295. * The display ordinal for the comment.
  1296. *
  1297. * @see comment_get_display_page()
  1298. */
  1299. function comment_get_display_ordinal($cid, $node_type) {
  1300. // Count how many comments (c1) are before $cid (c2) in display order. This is
  1301. // the 0-based display ordinal.
  1302. $query = db_select('comment', 'c1');
  1303. $query->innerJoin('comment', 'c2', 'c2.nid = c1.nid');
  1304. $query->addExpression('COUNT(*)', 'count');
  1305. $query->condition('c2.cid', $cid);
  1306. if (!user_access('administer comments')) {
  1307. $query->condition('c1.status', COMMENT_PUBLISHED);
  1308. }
  1309. $mode = variable_get('comment_default_mode_' . $node_type, COMMENT_MODE_THREADED);
  1310. if ($mode == COMMENT_MODE_FLAT) {
  1311. // For flat comments, cid is used for ordering comments due to
  1312. // unpredicatable behavior with timestamp, so we make the same assumption
  1313. // here.
  1314. $query->condition('c1.cid', $cid, '<');
  1315. }
  1316. else {
  1317. // For threaded comments, the c.thread column is used for ordering. We can
  1318. // use the sorting code for comparison, but must remove the trailing slash.
  1319. // See CommentRenderController.
  1320. $query->where('SUBSTRING(c1.thread, 1, (LENGTH(c1.thread) -1)) < SUBSTRING(c2.thread, 1, (LENGTH(c2.thread) -1))');
  1321. }
  1322. return $query->execute()->fetchField();
  1323. }
  1324. /**
  1325. * Returns the page number for a comment.
  1326. *
  1327. * Finds the correct page number for a comment taking into account display
  1328. * and paging settings.
  1329. *
  1330. * @param $cid
  1331. * The comment ID.
  1332. * @param $node_type
  1333. * The node type the comment is attached to.
  1334. *
  1335. * @return
  1336. * The page number.
  1337. */
  1338. function comment_get_display_page($cid, $node_type) {
  1339. $ordinal = comment_get_display_ordinal($cid, $node_type);
  1340. $comments_per_page = variable_get('comment_default_per_page_' . $node_type, 50);
  1341. return floor($ordinal / $comments_per_page);
  1342. }
  1343. /**
  1344. * Generates a comment preview.
  1345. *
  1346. * @param Drupal\comment\Comment $comment
  1347. */
  1348. function comment_preview(Comment $comment) {
  1349. global $user;
  1350. $preview_build = array();
  1351. if (!form_get_errors()) {
  1352. // Attach the user and time information.
  1353. if (!empty($comment->name->value)) {
  1354. $account = user_load_by_name($comment->name->value);
  1355. }
  1356. elseif ($user->uid && empty($comment->is_anonymous)) {
  1357. $account = $user;
  1358. }
  1359. if (!empty($account->uid)) {
  1360. $comment->uid->target_id = $account->uid;
  1361. $comment->name->value = check_plain($account->name);
  1362. }
  1363. elseif (empty($comment->name->value)) {
  1364. $comment->name->value = config('user.settings')->get('anonymous');
  1365. }
  1366. $comment->created->value = !empty($comment->created->value) ? $comment->created->value : REQUEST_TIME;
  1367. $comment->changed->value = REQUEST_TIME;
  1368. $comment->in_preview = TRUE;
  1369. $comment_build = comment_view($comment);
  1370. $comment_build['#weight'] = -100;
  1371. $preview_build['comment_preview'] = $comment_build;
  1372. }
  1373. if ($comment->pid->target_id) {
  1374. $build = array();
  1375. $comment = $comment->pid->entity;
  1376. if ($comment && $comment->status->value == COMMENT_PUBLISHED) {
  1377. $build = comment_view($comment);
  1378. }
  1379. }
  1380. else {
  1381. $build = node_view($comment->nid->entity);
  1382. }
  1383. $preview_build['comment_output_below'] = $build;
  1384. $preview_build['comment_output_below']['#weight'] = 100;
  1385. return $preview_build;
  1386. }
  1387. /**
  1388. * Implements hook_preprocess_HOOK() for block.html.twig.
  1389. */
  1390. function comment_preprocess_block(&$variables) {
  1391. if ($variables['configuration']['module'] == 'comment') {
  1392. $variables['attributes']['role'] = 'navigation';
  1393. }
  1394. }
  1395. /**
  1396. * Prepares a user account object for rendering comment authors.
  1397. *
  1398. * This helper handles anonymous authors in addition to registered comment
  1399. * authors.
  1400. *
  1401. * @return \Drupal\user\Plugin\Core\Entity\User
  1402. * A user account, for use with theme_username() or the user_picture template.
  1403. */
  1404. function comment_prepare_author(Comment $comment) {
  1405. // The account has been pre-loaded by CommentRenderController::buildContent().
  1406. $account = $comment->uid->entity;
  1407. if (!$account) {
  1408. $account = entity_create('user', array('uid' => 0, 'name' => $comment->name->value, 'homepage' => $comment->homepage->value));
  1409. }
  1410. return $account->getBCEntity();
  1411. }
  1412. /**
  1413. * Prepares variables for comment templates.
  1414. *
  1415. * Default template: comment.html.twig.
  1416. *
  1417. * @param array $variables
  1418. * An associative array containing:
  1419. * - elements: An associative array containing the comment and node objects.
  1420. * Array keys: #comment, #node.
  1421. */
  1422. function template_preprocess_comment(&$variables) {
  1423. $comment = $variables['elements']['#comment'];
  1424. $node = $variables['elements']['#node'];
  1425. $variables['comment'] = $comment;
  1426. $variables['node'] = $node;
  1427. $account = comment_prepare_author($comment);
  1428. // @todo Do not call theme() here. We do this for purposes of t().
  1429. $variables['author'] = theme('username', array('account' => $account));
  1430. $variables['new'] = $comment->new->value ? t('new') : '';
  1431. $variables['created'] = format_date($comment->created->value);
  1432. // Avoid calling format_date() twice on the same timestamp.
  1433. if ($comment->changed->value == $comment->created->value) {
  1434. $variables['changed'] = $variables['created'];
  1435. }
  1436. else {
  1437. $variables['changed'] = format_date($comment->changed->value);
  1438. }
  1439. if (theme_get_setting('features.comment_user_picture')) {
  1440. // To change user picture settings (e.g., image style), edit the 'compact'
  1441. // view mode on the User entity.
  1442. $variables['user_picture'] = user_view($account, 'compact');
  1443. }
  1444. else {
  1445. $variables['user_picture'] = array();
  1446. }
  1447. if (config('user.settings')->get('signatures') && !empty($account->signature)) {
  1448. $variables['signature'] = check_markup($account->signature, $account->signature_format, '', TRUE) ;
  1449. }
  1450. else {
  1451. $variables['signature'] = '';
  1452. }
  1453. $uri = $comment->uri();
  1454. $permalink_uri = $comment->permalink();
  1455. $uri['options'] += array('attributes' => array('class' => 'permalink', 'rel' => 'bookmark'));
  1456. $variables['title'] = l($comment->subject->value, $uri['path'], $uri['options']);
  1457. $variables['permalink'] = l(t('Permalink'), $permalink_uri['path'], $permalink_uri['options']);
  1458. $variables['submitted'] = t('Submitted by !username on !datetime', array('!username' => $variables['author'], '!datetime' => $variables['created']));
  1459. if ($comment->pid->target_id) {
  1460. // Fetch and store the parent comment information for use in templates.
  1461. $comment_parent = $comment->pid->entity;
  1462. $account_parent = comment_prepare_author($comment_parent);
  1463. $variables['parent_comment'] = $comment_parent;
  1464. // @todo Do not call theme() here. We do this for purposes of t().
  1465. $variables['parent_author'] = theme('username', array('account' => $account_parent));
  1466. $variables['parent_created'] = format_date($comment_parent->created->value);
  1467. // Avoid calling format_date() twice on the same timestamp.
  1468. if ($comment_parent->changed->value == $comment_parent->created->value) {
  1469. $variables['parent_changed'] = $variables['parent_created'];
  1470. }
  1471. else {
  1472. $variables['parent_changed'] = format_date($comment_parent->changed->value);
  1473. }
  1474. $permalink_uri_parent = $comment_parent->permalink();
  1475. $permalink_uri_parent['options'] += array('attributes' => array('class' => array('permalink'), 'rel' => 'bookmark'));
  1476. $variables['parent_title'] = l($comment_parent->subject->value, $permalink_uri_parent['path'], $permalink_uri_parent['options']);
  1477. $variables['parent_permalink'] = l(t('Parent permalink'), $permalink_uri_parent['path'], $permalink_uri_parent['options']);
  1478. $variables['parent'] = t('In reply to !parent_title by !parent_username',
  1479. array('!parent_username' => $variables['parent_author'], '!parent_title' => $variables['parent_title']));
  1480. }
  1481. else {
  1482. $variables['parent_comment'] = '';
  1483. $variables['parent_author'] = '';
  1484. $variables['parent_created'] = '';
  1485. $variables['parent_changed'] = '';
  1486. $variables['parent_title'] = '';
  1487. $variables['parent_permalink'] = '';
  1488. $variables['parent'] = '';
  1489. }
  1490. // Preprocess fields.
  1491. field_attach_preprocess($comment, $variables['elements'], $variables);
  1492. // Helpful $content variable for templates.
  1493. foreach (element_children($variables['elements']) as $key) {
  1494. $variables['content'][$key] = $variables['elements'][$key];
  1495. }
  1496. // Set status to a string representation of comment->status.
  1497. if (isset($comment->in_preview)) {
  1498. $variables['status'] = 'preview';
  1499. }
  1500. else {
  1501. $variables['status'] = ($comment->status->value == COMMENT_NOT_PUBLISHED) ? 'unpublished' : 'published';
  1502. }
  1503. // Gather comment classes.
  1504. $variables['attributes']['class'][] = 'comment';
  1505. // 'published' class is not needed, it is either 'preview' or 'unpublished'.
  1506. if ($variables['status'] != 'published') {
  1507. $variables['attributes']['class'][] = $variables['status'];
  1508. }
  1509. if ($variables['new']) {
  1510. $variables['attributes']['class'][] = 'new';
  1511. }
  1512. if (!$comment->uid->target_id) {
  1513. $variables['attributes']['class'][] = 'by-anonymous';
  1514. }
  1515. else {
  1516. if ($comment->uid->target_id == $variables['node']->uid) {
  1517. $variables['attributes']['class'][] = 'by-node-author';
  1518. }
  1519. if ($comment->uid->target_id == $variables['user']->uid) {
  1520. $variables['attributes']['class'][] = 'by-viewer';
  1521. }
  1522. }
  1523. // Add clearfix class.
  1524. $variables['attributes']['class'][] = 'clearfix';
  1525. $variables['content_attributes']['class'][] = 'content';
  1526. }
  1527. /**
  1528. * Returns HTML for a "you can't post comments" notice.
  1529. *
  1530. * @param $variables
  1531. * An associative array containing:
  1532. * - node: The comment node.
  1533. *
  1534. * @ingroup themeable
  1535. */
  1536. function theme_comment_post_forbidden($variables) {
  1537. $node = $variables['node'];
  1538. global $user;
  1539. // Since this is expensive to compute, we cache it so that a page with many
  1540. // comments only has to query the database once for all the links.
  1541. $authenticated_post_comments = &drupal_static(__FUNCTION__, NULL);
  1542. if (!$user->uid) {
  1543. if (!isset($authenticated_post_comments)) {
  1544. // We only output a link if we are certain that users will get permission
  1545. // to post comments by logging in.
  1546. $comment_roles = user_roles(TRUE, 'post comments');
  1547. $authenticated_post_comments = isset($comment_roles[DRUPAL_AUTHENTICATED_RID]);
  1548. }
  1549. if ($authenticated_post_comments) {
  1550. // We cannot use drupal_get_destination() because these links
  1551. // sometimes appear on /node and taxonomy listing pages.
  1552. if (variable_get('comment_form_location_' . $node->type, COMMENT_FORM_BELOW) == COMMENT_FORM_SEPARATE_PAGE) {
  1553. $destination = array('destination' => "comment/reply/$node->nid#comment-form");
  1554. }
  1555. else {
  1556. $destination = array('destination' => "node/$node->nid#comment-form");
  1557. }
  1558. if (config('user.settings')->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) {
  1559. // Users can register themselves.
  1560. return t('<a href="@login">Log in</a> or <a href="@register">register</a> to post comments', array('@login' => url('user/login', array('query' => $destination)), '@register' => url('user/register', array('query' => $destination))));
  1561. }
  1562. else {
  1563. // Only admins can add new users, no public registration.
  1564. return t('<a href="@login">Log in</a> to post comments', array('@login' => url('user/login', array('query' => $destination))));
  1565. }
  1566. }
  1567. }
  1568. }
  1569. /**
  1570. * Prepares variables for comment wrapper templates.
  1571. *
  1572. * Default template: comment-wrapper.html.twig.
  1573. *
  1574. * @param array $variables
  1575. * An associative array containing:
  1576. * - content: An associative array containing render arrays for the list of
  1577. * comments, and the comment form. Array keys: comments, comment_form.
  1578. */
  1579. function template_preprocess_comment_wrapper(&$variables) {
  1580. // Provide contextual information.
  1581. $variables['node'] = $variables['content']['#node'];
  1582. $variables['display_mode'] = variable_get('comment_default_mode_' . $variables['node']->type, COMMENT_MODE_THREADED);
  1583. // The comment form is optional and may not exist.
  1584. $variables['content'] += array('comment_form' => array());
  1585. $variables['attributes']['id'] = 'comments';
  1586. // Add a comment wrapper class.
  1587. $variables['attributes']['class'][] = 'comment-wrapper';
  1588. // Create separate variables for the comments and comment form.
  1589. $variables['comments'] = $variables['content']['comments'];
  1590. $variables['form'] = $variables['content']['comment_form'];
  1591. }
  1592. /**
  1593. * Returns an array of viewing modes for comment listings.
  1594. *
  1595. * We can't use a global variable array because the locale system
  1596. * is not initialized yet when the Comment module is loaded.
  1597. */
  1598. function _comment_get_modes() {
  1599. return array(
  1600. COMMENT_MODE_FLAT => t('Flat list'),
  1601. COMMENT_MODE_THREADED => t('Threaded list')
  1602. );
  1603. }
  1604. /**
  1605. * Returns an array of "comments per page" values that users can select from.
  1606. */
  1607. function _comment_per_page() {
  1608. return drupal_map_assoc(array(10, 30, 50, 70, 90, 150, 200, 250, 300));
  1609. }
  1610. /**
  1611. * Generates a sorting code.
  1612. *
  1613. * Consists of a leading character indicating length, followed by N digits
  1614. * with a numerical value in base 36 (alphadecimal). These codes can be sorted
  1615. * as strings without altering numerical order.
  1616. *
  1617. * It goes:
  1618. * 00, 01, 02, ..., 0y, 0z,
  1619. * 110, 111, ... , 1zy, 1zz,
  1620. * 2100, 2101, ..., 2zzy, 2zzz,
  1621. * 31000, 31001, ...
  1622. */
  1623. function comment_int_to_alphadecimal($i = 0) {
  1624. $num = base_convert((int) $i, 10, 36);
  1625. $length = strlen($num);
  1626. return chr($length + ord('0') - 1) . $num;
  1627. }
  1628. /**
  1629. * Decodes a sorting code back to an integer.
  1630. *
  1631. * @see comment_int_to_alphadecimal()
  1632. */
  1633. function comment_alphadecimal_to_int($c = '00') {
  1634. return base_convert(substr($c, 1), 36, 10);
  1635. }
  1636. /**
  1637. * Implements hook_ranking().
  1638. */
  1639. function comment_ranking() {
  1640. return array(
  1641. 'comments' => array(
  1642. 'title' => t('Number of comments'),
  1643. 'join' => array(
  1644. 'type' => 'LEFT',
  1645. 'table' => 'node_comment_statistics',
  1646. 'alias' => 'node_comment_statistics',
  1647. 'on' => 'node_comment_statistics.nid = i.sid',
  1648. ),
  1649. // Inverse law that maps the highest reply count on the site to 1 and 0 to 0.
  1650. 'score' => '2.0 - 2.0 / (1.0 + node_comment_statistics.comment_count * CAST(:scale AS DECIMAL))',
  1651. 'arguments' => array(':scale' => Drupal::state()->get('comment.node_comment_statistics_scale') ?: 0),
  1652. ),
  1653. );
  1654. }
  1655. /**
  1656. * Implements hook_rdf_mapping().
  1657. */
  1658. function comment_rdf_mapping() {
  1659. return array(
  1660. array(
  1661. 'type' => 'comment',
  1662. 'bundle' => RDF_DEFAULT_BUNDLE,
  1663. 'mapping' => array(
  1664. 'rdftype' => array('sioc:Post', 'sioct:Comment'),
  1665. 'title' => array(
  1666. 'predicates' => array('dc:title'),
  1667. ),
  1668. 'created' => array(
  1669. 'predicates' => array('dc:date', 'dc:created'),
  1670. 'datatype' => 'xsd:dateTime',
  1671. 'callback' => 'date_iso8601',
  1672. ),
  1673. 'changed' => array(
  1674. 'predicates' => array('dc:modified'),
  1675. 'datatype' => 'xsd:dateTime',
  1676. 'callback' => 'date_iso8601',
  1677. ),
  1678. 'comment_body' => array(
  1679. 'predicates' => array('content:encoded'),
  1680. ),
  1681. 'pid' => array(
  1682. 'predicates' => array('sioc:reply_of'),
  1683. 'type' => 'rel',
  1684. ),
  1685. 'uid' => array(
  1686. 'predicates' => array('sioc:has_creator'),
  1687. 'type' => 'rel',
  1688. ),
  1689. 'name' => array(
  1690. 'predicates' => array('foaf:name'),
  1691. ),
  1692. ),
  1693. ),
  1694. );
  1695. }
  1696. /**
  1697. * Implements hook_file_download_access().
  1698. */
  1699. function comment_file_download_access($field, EntityInterface $entity, File $file) {
  1700. if ($entity->entityType() == 'comment') {
  1701. if (user_access('access comments') && $entity->status->value == COMMENT_PUBLISHED || user_access('administer comments')) {
  1702. $node = $entity->nid->entity;
  1703. return node_access('view', $node);
  1704. }
  1705. return FALSE;
  1706. }
  1707. }
  1708. /**
  1709. * Implements hook_library_info().
  1710. */
  1711. function comment_library_info() {
  1712. $libraries['drupal.comment'] = array(
  1713. 'title' => 'Comment',
  1714. 'version' => VERSION,
  1715. 'js' => array(
  1716. drupal_get_path('module', 'comment') . '/comment-node-form.js' => array(),
  1717. ),
  1718. 'dependencies' => array(
  1719. array('system', 'jquery'),
  1720. array('system', 'drupal'),
  1721. array('system', 'drupal.form'),
  1722. ),
  1723. );
  1724. return $libraries;
  1725. }

Functions

Namesort descending Description
comment_add Returns a rendered form to comment the given node.
comment_alphadecimal_to_int Decodes a sorting code back to an integer.
comment_count_unpublished Returns a menu title which includes the number of unapproved comments.
comment_entity_bundle_info Implements hook_entity_bundle_info().
comment_field_extra_fields Implements hook_field_extra_fields().
comment_file_download_access Implements hook_file_download_access().
comment_form_node_form_alter Implements hook_form_BASE_FORM_ID_alter().
comment_form_node_type_form_alter Implements hook_form_FORM_ID_alter().
comment_get_display_ordinal Gets the display ordinal for a comment, starting from 0.
comment_get_display_page Returns the page number for a comment.
comment_get_recent Finds the most recent comments that are available to the current user.
comment_get_thread Retrieves comments for a thread.
comment_help Implements hook_help().
comment_int_to_alphadecimal Generates a sorting code.
comment_library_info Implements hook_library_info().
comment_links Adds reply, edit, delete, etc. links, depending on user permissions.
comment_load Loads the entire comment by comment ID.
comment_load_multiple Loads comment entities from the database.
comment_menu Implements hook_menu().
comment_menu_alter Implements hook_menu_alter().
comment_new_page_count Calculates the page number for the first new comment.
comment_node_insert Implements hook_node_insert().
comment_node_load Implements hook_node_load().
comment_node_page_additions Builds the comment-related elements for node detail pages.
comment_node_predelete Implements hook_node_predelete().
comment_node_prepare Implements hook_node_prepare().
comment_node_search_result Implements hook_node_search_result().
comment_node_type_delete Implements hook_node_type_delete().
comment_node_type_insert Implements hook_node_type_insert().
comment_node_type_load Loads the comment bundle name corresponding a given content type.
comment_node_type_update Implements hook_node_type_update().
comment_node_update_index Implements hook_node_update_index().
comment_node_view Implements hook_node_view().
comment_num_new Gets the number of new comments for the current user and the specified node.
comment_permalink Redirects comment links to the correct page depending on comment settings.
comment_permission Implements hook_permission().
comment_prepare_author Prepares a user account object for rendering comment authors.
comment_prepare_thread Calculates the indentation level of each comment in a comment thread.
comment_preprocess_block Implements hook_preprocess_HOOK() for block.html.twig.
comment_preview Generates a comment preview.
comment_ranking Implements hook_ranking().
comment_rdf_mapping Implements hook_rdf_mapping().
comment_theme Implements hook_theme().
comment_translation_configuration_element_submit Form submission handler for node_type_form().
comment_update_index Implements hook_update_index().
comment_uri Entity URI callback.
comment_user_cancel Implements hook_user_cancel().
comment_user_predelete Implements hook_user_predelete().
comment_view Generates an array for rendering a comment.
comment_view_multiple Constructs render array from an array of loaded comments.
template_preprocess_comment Prepares variables for comment templates.
template_preprocess_comment_wrapper Prepares variables for comment wrapper templates.
theme_comment_block Returns HTML for a list of recent comments.
theme_comment_post_forbidden Returns HTML for a "you can't post comments" notice.
_comment_body_field_create Creates a comment_body field instance for a given node type.
_comment_get_modes Returns an array of viewing modes for comment listings.
_comment_per_page Returns an array of "comments per page" values that users can select from.

Constants

Namesort descending Description
COMMENT_ANONYMOUS_MAYNOT_CONTACT Anonymous posters cannot enter their contact information.
COMMENT_ANONYMOUS_MAY_CONTACT Anonymous posters may leave their contact information.
COMMENT_ANONYMOUS_MUST_CONTACT Anonymous posters are required to leave their contact information.
COMMENT_FORM_BELOW Comment form should be shown below post or list of comments.
COMMENT_FORM_SEPARATE_PAGE Comment form should be displayed on a separate page.
COMMENT_MODE_FLAT Comments are displayed in a flat list - expanded.
COMMENT_MODE_THREADED Comments are displayed as a threaded list - expanded.
COMMENT_NODE_CLOSED Comments for this node are closed.
COMMENT_NODE_HIDDEN Comments for this node are hidden.
COMMENT_NODE_OPEN Comments for this node are open.
COMMENT_NOT_PUBLISHED Comment is awaiting approval.
COMMENT_PUBLISHED Comment is published.