Improving CP Comments View without Code
by Ryan Irelan
Recently, I found myself needing to tweak how the ExpressionEngine Control Panel displays the list of comments for an entry. The editors of a website I help maintain wanted to get a more complete overview of the comments on an entry, so they could quickly tell which are spam comments and mark them to be deleted.
In the Control Panel, there is the View Comments/Trackbacks page, which you can access by clicking the “View” in the “Comments” column of the Edit entry listing. But in order to see the entire comment, you have to click on the comment link, view the complete comment text and then go back to mark the comment as spam or delete it. When each entry receives dozens of comments, this quickly becomes a lot of clicking and tremendous wasted effort.
I was asked to make it easier to view all (or the majority) of the comment content right there in the table listing of comments on the View Comments page.
My first thought was to create a simple extension that replaced the contents of the cell with the full comment text. So, I jumped into the EE core code (cp.publish.php
to be exact) to find out where that table listing is built and see how it is created. This, known as “code diving”, is always a good way to get a bearing on the where you are in the application, how it is currently handling the functionality or display you want to replace or modify and, as was in this case, it can lead to some helpful discoveries.
While hunting around the cp.publish.php
file and the Publish
class, I realized that there was a hook, view_comments_start
, to completely rewrite the View Comments/Trackbacks page.
// -------------------------------------------
// 'view_comments_start' hook.
// - Allows complete rewrite of View Comments/Trackbacks page.
//
$edata = $EXT->call_extension('view_comments_start', $weblog_id, $entry_id, $message, $show_trackbacks, $id_array, $pagination_links, $rownum);
if ($EXT->end_script === TRUE) return;
//
// -------------------------------------------
I hadn’t found this hook on my previous scan of the Extension Hooks list, so it was good to know that if I needed to create an extension, I could. However, building an extension for this request seemed like a lot of code to write for such a simple tweak.
After poking around the Publish
class some more, I came across a list of class variables, which are defined right at the top of the class. Two of those listed are:
var $comment_chars = 50;
var $comment_leave_breaks = 'n';
Right then I knew that is exactly what I needed. In my locally running copy of the website, I tweaked those variable values to see how they impacted the display of the comment text on the View Comments/Trackbacks page.
$comment_chars
sets the number of characters of a comment to display and $comment_leave_breaks
is a y/n setting on whether to use or strip out the line breaks in the comment text.
By setting the value of $comment_chars
to 500
and the value of $comment_leave_breaks
to y
, I was able to get the display roughly how I wanted it: almost the entire comment text would display (at least enough to determine whether or not it is spam) and the line breaks would be intact, so the comments are properly formatted.
That’s exactly how I wanted it to look. However, I’ve now edited the core EE code, which while not harmful, isn’t really a best practice. A future update of EE could break my changes and multiple edits of the core files can be extremely difficult to track.
So, what to do?
Luckily, there is another way of altering those values without editing the cp.publish.php
file. I found the answer right there in the code:
/* -------------------------------------------
/* Hidden Configuration Variables
/* - view_comment_chars => Number of characters to display (#)
/* - view_comment_leave_breaks => Create <br />'s based on line breaks? (y/n)
/* -------------------------------------------*/
ExpressionEngine has come “hidden” configuration variables that you can use in your config.php
file.
$conf['view_comment_chars'] = "500";
$conf['view_comment_leave_breaks'] = "y";
With these two new (to me) variables, I could set the display of the Comments View page without writing any additional code or editing the cp.publish.php
file.
This, I think, was the ideal solution. Even better is that I found it by a natural process of identifying the problem, diving into the code to learn more and then discovery of a better way to solve my problem.