ryan

Quick guide to the comments system

Recommended Posts

Hello there,

is there a possibility to change the text of the email which is sent after a new comment is posted? I would like to give mny costumer a direct link to approve the entry...

Cheers,

Christian

Share this post


Link to post
Share on other sites

@doolak not that I'm aware of. I'm sure there is a thread here where someone is looking to customize the comments operation and Ryan/others answer the point and if I remember it (then) required a customization to the core (if I remember correctly).

Share this post


Link to post
Share on other sites

For that need, I would just modify the existing Comments module. You'll find the sendNotificationEmail() method in FieldtypeComments.module and it's pretty straightforward and easy to modify. Keep a copy of it separately from your installation as well so that you can plug it back into place after doing PW version upgrades.

Share this post


Link to post
Share on other sites

That was great tutorial. I am using modified version of the comments as a guestbook page. Now as I have 300+ entries I would love to see pagination. Or does anyone know nice jQuery plugin which loads paragraphs on demand (infinite scrolling), like JAIL does for images (although not sure it is only for images)? I would appreciate if someone with more insight might give me a pointer.

Share this post


Link to post
Share on other sites

I'm not familiar with a jQuery plugin like that, but would also be interested in learning more about it if anyone does know of something like this.

Share this post


Link to post
Share on other sites

Hi chaps

I noticed that if the user refreshes the page the comments get added ad infinitum.

Is there a simple check that can be done to:

  1. Stop the user posting the same content twice in a row (presumably grab their previous comment for a page and check the content isn't exactly the same
  2. Build on that and add a time period that must pass before they can add a comment to the same post or another post (thinking 30 seconds for both is pretty standard, but would be nice to have the option to have them separate)
  3. Currently I have it set to require approval if they have not posted before, but let their posts through after that - very brave of me - but the message is the same both times - "thank you for your submission". Can we change the message so if it's pending approval it actually says that so they're not tempted to refresh the page as that's what I did ;)

I'm not sure whether these have been suggested before so any input is appreciated.

Share this post


Link to post
Share on other sites

Pete, these updates are now in the FieldtypeComments for the dev branch. Please try it out and let me know how it works for you. I've also added a 'website' field option to the field settings. After you've updated, you'll want to edit your comments field(s) settings to enable the 'redirect after post' option.

  • Like 2

Share this post


Link to post
Share on other sites

Thanks ryan

Works locally, but when I try and upload the new wire directory from either the master or dev branch to my server I get an odd error:

2012-11-18 11:03:34    ?    http://www.mytsite.com/?/    Compile Error    Cannot redeclare class SessionLoginThrottle (line 17 of /home/mysite/public_html/wire/modules/SessionLoginThrottle.module)

I'm sure it must be a hosting issue since it works locally, but any ideas what could cause that?

Share this post


Link to post
Share on other sites
Works locally, but when I try and upload the new wire directory from either the master or dev branch to my server I get an odd error:

you will.do

rm /site/assets/cache/Modules*.cache
  • Like 1

Share this post


Link to post
Share on other sites

Sadly that didn't work :(

Okay, that's freaky - I then deleted the uploaded wire folder from the dev branch, re-uploaded it and now it works.

I swear I tried deleting the cache several times before. I guess the FTP upload could have been interrupted. Oh well, works now! :)

Share this post


Link to post
Share on other sites

Ah, next problem is that the new version fails to store the name, email address or message :(

Share this post


Link to post
Share on other sites

Pete, I think I may have this fixed. Though couldn't exactly seem to duplicate it here, but found something that looks like it could be responsible (some missing parenthesis). Would you mind trying again with the latest dev version?

Share this post


Link to post
Share on other sites

Gentlemen.

What's the word on comment pagination? I'm facing a situation right now in which paginating comments would be very useful. If a page receives 50 lengthy comments it makes for a lot of scrolling. It was on the roadmap in 2011 but I have not seen anything on whether it has been implemented.

If not, is there a possibility of generating your own markup for the comments and making it paginate in the same way you do with search results?

Edit: I've been able to solve a lot of my own problems lately. Maybe that's a good sign. I realized that pagination can be achieved through jquery so I am going to implement that. I'll let you know how it goes for anyone else looking to do the same thing.

Edited by digitex

Share this post


Link to post
Share on other sites

Follow up note:

jquery pagination for comments is very helpful. It keeps things nicely organized and compact.

I used Pajinate but there are quite a few options. I'm probably not telling most on this board anything they didn't already know but for those that don't... there you go.

  • Like 1

Share this post


Link to post
Share on other sites

You might want to check out the blog profile, which uses "next/prev" for comments pagination:

http://processwire.com/blogtest/comments/

Also, the current version of ProcessWire does include the ability to paginate comments, though it's not particularly automated yet. See the FieldtypeComments::findComments function. While not the prettiest bit of API code, you can use it like this:

$limit = 10; // comments to display per page
$start = ($input->pageNum-1) * $limit; 
$selector = "page=$page, sort=-created, start=$start, limit=" . ($limit+1); 

// find the comments. replace "comments" with the name of your comments field
$comments = FieldtypeComments::findComments("comments", $selector); 

// output the comments
echo $comments->render();

// output the pagination links
if($input->pageNum > 1) echo "<a href='./page" . ($input->pageNum-1) . "'>Back</a> ";
if(count($comments) > $limit) echo "<a href='./page" . ($input->pageNum+1) . "'>Next</a>";
  • Like 4

Share this post


Link to post
Share on other sites

Hey,

i have to style the comment form in a other way so that it fits into the design of the page.

It has to be just other HTML and CSS, the fields will be the same.

Any hints how to do this?

Do i have to edit the code for renderForm()?

Thank you for your support!

Share this post


Link to post
Share on other sites

See this Comments Fieldtype documentation page sections on customizing output and styling output. While it makes it easier, you don't actually have to use the built in methods for rendering the comments list or form. You can create your own markup entirely if preferred. However, I do think it would be worthwhile to copy or extend ProcessWire's /wire/modules/Fieldtype/FieldtypeComments/CommentForm.php file to your own class (MyCommentForm) in /site/templates/. When you want to render your form, you would do this:

include("./MyCommentForm.php");
$form = new MyCommentForm($page, $page->comments); 
echo $form->render(); 
  • Like 4

Share this post


Link to post
Share on other sites

Happy new year guys!

Hope one of you might be able to help me with this snag...

Like Kent above, I would like to modify the html and css of the comment form.

Rather then modify the core one, I wanted to make a copy of the core version and run off that for the PW blog profile.

Following the example above, this is what I did after creating and placing a copy of the core comments form...

// render our blog post and comments

include("./CommentFormCustom.php");
$form = new CommentFormCustom($page, $page->comments);  

$content = renderPosts($page) . $form->render() . renderNextPrevPosts($page);

Once I do this, I get the following error message:

Compile Error: Cannot redeclare class CommentFormInterface (line 24 of /Applications/MAMP/htdocs/pwblog.dev/site/templates/CommentFormCustom.php) 

Any ideas what I did wrong or how I can solve this?

Running latest version of 2.3 Dev

Cheers!

Share this post


Link to post
Share on other sites

Happy new year!

What the error is saying is that you are trying to call a class that has already been called (there's a better way to express this but Google is your friend). There's various ways to solve this issue:

include_once: http://www.php.net/manual/en/function.include-once.php

require_once: http://www.php.net/manual/en/function.require-once.php

Another way around this is when you make copies of existing classes, to rename them...e.g. CommentFormCustom to MyCommentFormCustom. 

The experts here will give you better answers... :-)....

Further reading.

http://stackoverflow.com/questions/708140/php-fatal-error-cannot-redeclare-class

http://stackoverflow.com/questions/2418473/when-should-i-use-require-once-vs-include

http://stackoverflow.com/questions/2595171/php-include-once

http://www.w3schools.com/php/php_includes.asp

http://www.w3resource.com/php/statement/require_once.php

  • Like 4

Share this post


Link to post
Share on other sites

Hi Kongondo

Thanks for the reply and assistance. Much appreciated mate!

Renaming the classes is what I needed.

Cheers!

Share this post


Link to post
Share on other sites

Hey Guys

Hopefully one of gents can help me with a question...

Earlier in this thread, Ryan submitted this really helpful code snippet that enabled us to paginate our comments list...

$limit = 20; // comments to display per page
$start = ($input->pageNum-1) * $limit; 
$selector = "page=$page, sort=-created, start=$start, limit=" . ($limit+1); 

// find the comments. replace "comments" with the name of your comments field
$comments = FieldtypeComments::findComments("comments", $selector); 

// output the comments
echo $comments->render();

// output the pagination links
if($input->pageNum > 1) echo "<a href='./page" . ($input->pageNum-1) . "'>Back</a> ";
if(count($comments) > $limit) echo "<a href='./page" . ($input->pageNum+1) . "'>Next</a>";

The above snippet will generate a paginated list of comments using the standard render functions output..... This works great!

Ryan also gave us another great snippet that enabled us to help generate our own custom html output...

foreach($page->comments as $comment) {
    if($comment->status < 1) continue; // skip unapproved or spam comments
    $cite = htmlentities($comment->cite); // make sure output is entity encoded
    $text = htmlentities($comment->text);
    $date = date('m/d/y g:ia', $comment->created); // format the date
    echo "<p><strong>Posted by $cite on $date</strong><br />$text</p>";

This work great too!

My question is to all you helpful gents is... how do we merge these two snippets together such that we have pagination of comments along with our own custom output?

Thanks guys!

Share this post


Link to post
Share on other sites

Doh...

Solved my own problem....

Changed this... foreach($page->comments as $comment)

To this... foreach($comments as $comment)

Sorry for diverting you here... but thanks for looking!

Here is what I have ended up with...

// DISPLAY PAGINATED COMMENTS

$limit = 10; // comments to display per page
$start = ($input->pageNum-1) * $limit; 
$selector = "page=$page, sort=-created, start=$start, limit=" . ($limit+1); 

// find the comments. replace "comments" with the name of your comments field
$comments = FieldtypeComments::findComments("comments", $selector); 

// output the comments
foreach($comments as $comment) {
    if($comment->status < 1) continue; // skip unapproved or spam comments
    $cite = htmlentities($comment->cite); // make sure output is entity encoded
    $text = htmlentities($comment->text);
    $date = date('m/d/y g:ia', $comment->created); // format the date
    echo "<p>Posted by $cite on $date<br />$text</p>";
}

// output the pagination links
if($input->pageNum > 1) echo "<a href='./page" . ($input->pageNum-1) . "'>Back</a> ";
if(count($comments) > $limit) echo "<a href='./page" . ($input->pageNum+1) . "'>Next</a>";

Thanks guys

  • Like 5

Share this post


Link to post
Share on other sites

Not sure if this is the right place but it would be very nice to add admin panel (gui) for the FieldtypeComments module so that the site users could easily change all the hardcoded markup text, e.g. submit button text, success/error messages etc. At present they require hacking.

On this note - what would be the quickest and most elegant way to change the "Submit" button label? (I know I can change it in the module php, but is there a better way?)

many thanks

Share this post


Link to post
Share on other sites

It's normally not the job of the user to care about markup. That's normally a one time job as your styling does depent on it.

And by no means it's "hacking" as you can change the markup from whereever you want to output comments. It's shown here how to do it in different ways: http://processwire.com/api/fieldtypes/comments/ The only thing that's hardcoded is the default markup. If it's about the ease of just calling ->render(). You could always make a own function or hook the render method itself to inject an array with markup. It's up to you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.