Comment Count In Pagelist

Permalink 1 user found helpful
I am trying to get the comment count in pagelist.

This is the default template. Apparently the method does not exist because it is not returning anything.

if(method_exists($entryController,'getCommentCountString')) {
         $comments = $entryController->getCommentCountString('%s '.t('Comment'), '%s '.t('Comments'));
      }


Any Ideas?

ministrycrm
View Replies: View Best Answer
jordanlev replied on at Permalink Reply 1 Attachment
jordanlev
Yeah, that code only works for the built-in blog_entry page type. Attached is a helper file with some code that will do this for you on any page type. To use it, download it and unzip, then place the unzipped "comment_count.php" file in your site's top-level "helpers" directory (*not* concrete/helpers).

Then in your page list template, put this code *before* the foreach loop starts:
$cc_phrase_singular = '%d comment';
$cc_phrase_plural = '%d comments';
$cc_phrase_disabled = 'Comments disabled';
$cch = Loader::helper('comment_count');

(you'll want to customize the first 3 lines depending on what messages you want to display -- the %d thing is a placeholder where the comment count number will go so you need that in the first two).

Then put this code *inside* the foreach loop, where you want the message outputted:
echo $cch->comment_count_string($page, $cc_phrase_singular, $cc_phrase_plural, $cc_phrase_disabled);


Note that this code is from a ways back, so I'm not 100% sure it still works... but it probably does.

-Jordan
nickratering replied on at Permalink Reply
nickratering
Has anybody got a comment counter in a pagelist working in 5.6?
There are many postst about this subject, but I can't get one to work.

Best regards,
Nick
jordanlev replied on at Permalink Reply
jordanlev
I just tested this out on Concrete5.6.1 and it worked fine. What exactly is the problem you're having? Can you post the code that you're trying to use?
nickratering replied on at Permalink Best Answer Reply
nickratering
I used the code exact as you described, in a default page list. I get the 'comments disabled' message, but I've added and approved three comments. Thnx!

EDIT: if I comment out the if statement in the helper, the counter works:
public function comment_count_string($collectionObject, $singular_format, $plural_format, $disabled_message = '') {
      //if ($this->comments_enabled($collectionObject)) {
         $count = $this->comment_count($collectionObject);
         $format = ($count == 1 ? $singular_format : $plural_format);
         return sprintf($format, $count);
      //} else {
      //   return $disabled_message;
      //}
   }


Pagelist:
<?php 
defined('C5_EXECUTE') or die("Access Denied.");
$rssUrl = $showRss ? $controller->getRssUrl($b) : '';
$th = Loader::helper('text');
$cc_phrase_singular = '%d comment';
$cc_phrase_plural = '%d comments';
$cc_phrase_disabled = 'Comments disabled';
$cch = Loader::helper('comment_count');
?>
<div class="ccm-page-list">
   <?php  foreach ($pages as $page):
      $title = $th->entities($page->getCollectionName());
      $url = $nh->getLinkToCollection($page);
      $target = ($page->getCollectionPointerExternalLink() != '' && $page->openCollectionPointerExternalLinkInNewWindow()) ? '_blank' : $page->getAttribute('nav_target');
      $target = empty($target) ? '_self' : $target;
jordanlev replied on at Permalink Reply
jordanlev
Sorry, but I don't know why it wouldn't be working properly on your site. Your solution to the problem looks like a good one though.
losttheplot replied on at Permalink Reply
losttheplot
Thank you very much for posting this excellent little helper and calling code, which just saved me a stack of time trying to work out how to fetch the number of approved posts :)

[ Works with 5.6.2.2b2 ]
seniorb replied on at Permalink Reply
This is a really helpful thread; having used the comment_count.php and also the changes to the pagelist custom template I thought I'd post a couple of additions to them, to show not only the comment count, but the last comment author and date.

Firstly, here's the comment_count.php file which should go in your helpers folder (not /concrete/helpers);
<?php defined('C5_EXECUTE') or die("Access Denied.");
class CommentCountHelper {
   public function comment_count_string($collectionObject, $singular_format, $plural_format, $disabled_message = '') {
      //if ($this->comments_enabled($collectionObject)) {
         $count = $this->comment_count($collectionObject);
         $format = ($count == 1 ? $singular_format : $plural_format);
         return sprintf($format, $count);
      //} else {
      //   return $disabled_message;
      //}
   }
   public function comment_get_last($collectionObject) {
      //if ($this->comments_enabled($collectionObject)) {
         $count = $this->comment_count($collectionObject);
         if ($count > 0)


Now create a new pagelist template - I put a new folder called "mytemplate" here; .\blocks\page_list\templates and added a view.php in this folder. Here's the code for the view.php;
<?php 
defined('C5_EXECUTE') or die("Access Denied.");
$rssUrl = $showRss ? $controller->getRssUrl($b) : '';
$th = Loader::helper('text');
$cc_phrase_singular = '%d comment';
$cc_phrase_plural = '%d comments';
$cc_phrase_disabled = 'Comments disabled';
$cch = Loader::helper('comment_count');
?>
<div class="ccm-page-list">
   <?php  foreach ($pages as $page):
      $title = $th->entities($page->getCollectionName());
      $url = $nh->getLinkToCollection($page);
      $date = $page->getCollectionDatePublic(DATE_APP_GENERIC_MDY_FULL);
      $author = $page->getVersionObject()->getVersionAuthorUserName();


Hope that saves someone a little time.