How do I show all comments of the node without the pager (300+
comments)?
This is a desired function knowing the negative impact on UX.
I highly advice not to display/load 300 comments at the same time, not only it will make your page slower, but also it will increase your server costs. Will all users read all 300 comments? I doubt it, maybe a few might.
use an endless scroll to get rid of the pager.
Sadly I'm not familiar with the implementation of endless scroll widgets. – Eyal
This is the right approach. All you need is a little jQuery magic using AJAX load() method
First, set comments per page to 10.
Second, you need to modify the comments-wrapper.tpl.php file. This is bartik's:
<div id="comments" class="<?php print $classes; ?>"<?php print $attributes; ?>>
<?php if ($content['comments'] && $node->type != 'forum'): ?>
<?php print render($title_prefix); ?>
<h2 class="title"><?php print t('Comments'); ?></h2>
<?php print render($title_suffix); ?>
<?php endif; ?>
<div class="ajax-wrap"> // added this wrapper
<?php print render($content['comments']); ?>
<div class="loadmore"></div> // added this div to load the next page stuff here
</div> // closes ajax-wrap
<?php if ($content['comment_form']): ?>
<h2 class="title comment-form"><?php print t('Add new comment'); ?></h2>
<?php print render($content['comment_form']); ?>
<?php endif; ?>
</div>
Added the ajax-wrap
div since there was no wrapper that only wrapped the comments and the pager as we don't want the "add comment box/form". We will load the next page comments inside loadmore
div.
Third, we are going to use jQuery Visible to check if the loadmore
div is visible on your browser window. If it's visible, we are going to load the next page comments using AJAX load() method
(function ($) {
!function(t){var i=t(window);t.fn.visible=function(t,e,o){if(!(this.length<1)){var r=this.length>1?this.eq(0):this,n=r.get(0),f=i.width(),h=i.height(),o=o?o:"both",l=e===!0?n.offsetWidth*n.offsetHeight:!0;if("function"==typeof n.getBoundingClientRect){var g=n.getBoundingClientRect(),u=g.top>=0&&g.top<h,s=g.bottom>0&&g.bottom<=h,c=g.left>=0&&g.left<f,a=g.right>0&&g.right<=f,v=t?u||s:u&&s,b=t?c||a:c&&a;if("both"===o)return l&&v&&b;if("vertical"===o)return l&&v;if("horizontal"===o)return l&&b}else{var d=i.scrollTop(),p=d+h,w=i.scrollLeft(),m=w+f,y=r.offset(),z=y.top,B=z+r.height(),C=y.left,R=C+r.width(),j=t===!0?B:z,q=t===!0?z:B,H=t===!0?R:C,L=t===!0?C:R;if("both"===o)return!!l&&p>=q&&j>=d&&m>=L&&H>=w;if("vertical"===o)return!!l&&p>=q&&j>=d;if("horizontal"===o)return!!l&&m>=L&&H>=w}}}}(jQuery);
// The above lines of code is the minified version of jQuery Visible.js see https://www.customd.com/articles/13/checking-if-an-element-is-visible-on-screen-using-jquery
(function Forever(){ // we create a function called Forever
var graburl = $('.pager-next a').attr('href'); // we create a variable called graburl which gets the url inside the href of the pager-next div class
$('#comments .pager').css("display", "none"); // hides the comments pager div
if ($('.loadmore').visible(true) && graburl != null) { // we check if the load here div is visible and also if the grab url is not empty (will be empty when we reach the last page, since there is no "next" button.
$('.pager').remove(); // we remove/delete the pager from our website as we already grab the href with graburl variable and also we don't need this pager anymore.
$('.loadmore').html('<center><img src="https://i.stack.imgur.com/RCEAe.gif"></center>').load('' + graburl + ' .ajax-wrap').attr("class", "loaded"); // We place a loading gif. Then load the content inside the ajax-wrap div of "next page". Then we rename the div loadmore to loaded.
}
setTimeout(Forever, 1); // if it was 1000 we would run this code every 1 second, but since it is 1, we run this code every millisecond. Basically we are looping this code.
})();
})(jQuery);
The cool thing, if for some reason the user browser blocks jQuery, they will be able to browse the comments using the pager.
PS: In comments-wrapper.tpl.php you should move the comment form above the comments, that way people don't have to scroll all the way to the end to get the comment box. (something I did not do)