1 comment.module comment_node_page_additions(Node $node)

Builds the comment-related elements for node detail pages.

Parameters

Node $node: The node entity for which to build the comment-related elements.

Return value

A renderable array representing the comment-related page elements for the: node.

File

core/modules/comment/comment.module, line 746
Enables users to comment on published content.

Code

function comment_node_page_additions(Node $node) {
  $node_type = node_type_get_type($node->type);

  $additions = array(
    'comments' => array(),
    'comment_form' => array(),
  );

  // Only attempt to render comments if the node has visible comments.
  // Unpublished comments are not included in $node->comment_count, so show
  // comments unconditionally if the user is an administrator.
  if ((!empty($node->comment_count) && user_access('access comments')) || user_access('administer comments')) {
    $mode = $node_type->settings['comment_mode'];
    $comments_per_page = $node_type->settings['comment_per_page'];
    if ($cids = comment_get_thread($node, $mode, $comments_per_page)) {
      $comments = comment_load_multiple($cids);
      // In rare cases $comments is an empty array,
      // for instance if the only comment entity attached to this node
      // is broken for some reason.
      if (!empty($comments)) {
        comment_prepare_thread($comments);
        $build = comment_view_multiple($comments, $node);
        $build['pager']['#theme'] = 'pager';
        $additions['comments'] = $build;
      }
      elseif ($node->comment_count > 0) {
        // Comment count has got out of line, update it.
        db_update('node_comment_statistics')
          ->fields(array(
            'cid' => 0,
            'comment_count' => 0,
            'last_comment_timestamp' => $node->created,
            'last_comment_name' => '',
            'last_comment_uid' => $node->uid,
          ))
          ->condition('nid', $node->nid)
          ->execute();
      }
    }
  }

  // Append comment form if needed.
  if (Comment::createAccess() && $node->comment == COMMENT_NODE_OPEN && ($node_type->settings['comment_form_location'] == COMMENT_FORM_BELOW)) {
    $comment = entity_create('comment', array('nid' => $node->nid));
    $additions['comment_form'] = backdrop_get_form("comment_node_{$node->type}_form", $comment);
  }

  return $additions;
}