CommentRenderController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @file
  4. * Definition of Drupal\comment\CommentRenderController.
  5. */
  6. namespace Drupal\comment;
  7. use Drupal\Core\Entity\EntityInterface;
  8. use Drupal\Core\Entity\EntityRenderController;
  9. use Drupal\entity\Plugin\Core\Entity\EntityDisplay;
  10. /**
  11. * Render controller for comments.
  12. */
  13. class CommentRenderController extends EntityRenderController {
  14. /**
  15. * Overrides Drupal\Core\Entity\EntityRenderController::buildContent().
  16. *
  17. * In addition to modifying the content key on entities, this implementation
  18. * will also set the node key which all comments carry.
  19. */
  20. public function buildContent(array $entities, array $displays, $view_mode, $langcode = NULL) {
  21. $return = array();
  22. if (empty($entities)) {
  23. return $return;
  24. }
  25. // Pre-load associated users into cache to leverage multiple loading.
  26. $uids = array();
  27. foreach ($entities as $entity) {
  28. $uids[] = $entity->uid->target_id;
  29. }
  30. user_load_multiple(array_unique($uids));
  31. parent::buildContent($entities, $displays, $view_mode, $langcode);
  32. // Load all nodes of all comments at once.
  33. $nids = array();
  34. foreach ($entities as $entity) {
  35. $nids[$entity->nid->target_id] = $entity->nid->target_id;
  36. }
  37. $nodes = node_load_multiple($nids);
  38. foreach ($entities as $entity) {
  39. if (isset($nodes[$entity->nid->target_id])) {
  40. $node = $nodes[$entity->nid->target_id];
  41. }
  42. else {
  43. throw new \InvalidArgumentException(t('Invalid node for comment.'));
  44. }
  45. $entity->content['#node'] = $node;
  46. $entity->content['#theme'] = 'comment__node_' . $node->bundle();
  47. $entity->content['links'] = array(
  48. '#theme' => 'links__comment',
  49. '#pre_render' => array('drupal_pre_render_links'),
  50. '#attributes' => array('class' => array('links', 'inline')),
  51. );
  52. if (empty($entity->in_preview)) {
  53. $entity->content['links'][$this->entityType] = array(
  54. '#theme' => 'links__comment__comment',
  55. // The "node" property is specified to be present, so no need to check.
  56. '#links' => comment_links($entity, $node),
  57. '#attributes' => array('class' => array('links', 'inline')),
  58. );
  59. }
  60. }
  61. }
  62. /**
  63. * Overrides Drupal\Core\Entity\EntityRenderController::alterBuild().
  64. */
  65. protected function alterBuild(array &$build, EntityInterface $comment, EntityDisplay $display, $view_mode, $langcode = NULL) {
  66. parent::alterBuild($build, $comment, $display, $view_mode, $langcode);
  67. if (empty($comment->in_preview)) {
  68. $prefix = '';
  69. $is_threaded = isset($comment->divs)
  70. && variable_get('comment_default_mode_' . $comment->bundle(), COMMENT_MODE_THREADED) == COMMENT_MODE_THREADED;
  71. // Add 'new' anchor if needed.
  72. if (!empty($comment->first_new)) {
  73. $prefix .= "<a id=\"new\"></a>\n";
  74. }
  75. // Add indentation div or close open divs as needed.
  76. if ($is_threaded) {
  77. $build['#attached']['css'][] = drupal_get_path('module', 'comment') . '/comment.theme.css';
  78. $prefix .= $comment->divs <= 0 ? str_repeat('</div>', abs($comment->divs)) : "\n" . '<div class="indented">';
  79. }
  80. // Add anchor for each comment.
  81. $prefix .= "<a id=\"comment-{$comment->id()}\"></a>\n";
  82. $build['#prefix'] = $prefix;
  83. // Close all open divs.
  84. if ($is_threaded && !empty($comment->divs_final)) {
  85. $build['#suffix'] = str_repeat('</div>', $comment->divs_final);
  86. }
  87. }
  88. }
  89. }