Jump to content

Comments Module Modification


nikola
 Share

Recommended Posts

I have a situation where I need comments without name & e-mail inputs, just comments textarea for submitting comments and I've managed to strip those out from the module and it's working fine.

Instead of showing name that is submitted through the name inputfield, I need to show the name of current logged in user who writes the comment.

Also, new posted comment would have to be displayed instantly without showing "Thank you, your submission has been saved." and without refreshing the page (through ajax request).

The name of logged in user would have to be visible in comments admin instead name & e-mail inputfields (which are stripped out).

Thanks for your help guys.

Link to comment
Share on other sites

The existing comments module does keep the user ID already, though it doesn't use it for storing the user name. But you could make an autoload module that changes the 'cite' property of a comment to the user name. This is something that has to be done at runtime since a user's name may have changed since the comment was posted.

public function init() {
$this->addHookAfter('FieldtypeComments::wakeupValue', $this, 'hookWakeupValue');
}

public function hookWakeupValue(HookEvent $event) {
$value = $event->return;
foreach($value as $comment) {
	if($comment->user) $comment->cite = $comment->user->name;
}
}

To have the comment displayed instantly, that will be easier to solve, and doesn't require modifying the existing module. Just do your comments processing/output generation before you "echo" any output. When a comment is posted, do a $session->redirect('./');

I'm making a page-based comments module which will have the same sort of flexibility as the rest of ProcessWire, though it'll be a bit before I've got it finished (and some projects have to come first). But this page-based comments module should support nearly anything you could want to do with it, without having to modify its core code.

  • Like 2
Link to comment
Share on other sites

  • 2 months later...

Hello there!

First:

Happy Eastern!

Second:

I want to have the success message and the error message displayed via Javascript, without reloading the page - but i have no idea how i could integrate this without modifying the module.

Can this be done with a hook somehow?

Kind regards,

Christian

  • Like 1
Link to comment
Share on other sites

Christian,

This is something you don't need a hook for, rather this is more about jQuery. You'll use jQuery to take over the submit() event to the form. Create a new template and page to handle your comment posts. Turn on URL segments for the template, as you will use a URL segment to tell the comment template which page comments are being posted for. Here are some code samples on how you might do this, though this is written in the browser off my memory, so you may need to tweak before it works.

Template: post-comment.php (used by page /tools/post-comment/)

$fieldName = 'comments';
$id = (int) $input->urlSegment1; 
$p = $pages->find($id)->first(); // using find() rather than get() to ensure access checking
if(!$p || !$p->has($fieldName)) throw new WireException("Invalid page");
$form = $p->get($fieldName)->getCommentForm(); 
$out = $form->render();

if($config->ajax) {
   echo $out;
} else {
   // handle non-JS submission
   include("./head.inc"); 
   echo $out;
   include("./foot.inc"); 
}

Template: basic-page.php (or whatever template(s) are used by your pages with comments):

// render the list of comments
$comments = $page->comments->render();

// render the comments form, telling it to submit to our /tools/comments/ page instead
$comments .= $page->comments->renderForm(array(
   // tell it not to process comments here
   'processInput' => false, 
   // submit to /tools/comments/[page_id]
   'attrs' => array('action' => $config->urls->root . 'tools/comments/' . $page->id) 
   ));

$comments .= <<< _OUT

<script type='text/javascript'>
   // you may prefer to move this to a separate JS file
   $(document).ready(function() {
       var $form = $('#CommentForm'); 
       $form.submit(function() {
           $.post($form.attr('action'), $form.serialize(), function(data) {
               $form.prepend(data); 
           }); 
           return false; 
       }); 
   }); 
</script>

_OUT;

include("./head.inc");
echo $page->body; // replace with your page output
echo $comments; // output the comments and form
include("./foot.inc"); 
Link to comment
Share on other sites

  • 3 weeks later...

Hi Ryan,

I'm trying to do something similar to Nikola — bypass the name and email inputs and just have the comment be attributed to the currently logged in user.

I created an autoload module like you suggested above, but all my comments are now attributed to "guest" whether the user is logged in or not.

I'd also like to do a $session->redirect('./'); on success instead of showing a message, but I'm a bit confused on how to achieve that without modifying the module.

Any additional help on this would be appreciated.

Update

I found this post about how to do the redirect, so I was able to implement that part.

Link to comment
Share on other sites

For a quick solution:

• Populate the name and email inputs with the current user info via jQuery.

• Hide name and email inputs via CSS

For those who might be interested here's a quick rundown of what I did.

jQuery to populate the fields

var name = '<?php echo $user->name ?>';
var email = '<?php echo $user->email ?>';
$('#CommentForm_cite').val(name);
$('#CommentForm_email').val(email);

CSS to hide the fields and labels

#CommentForm_cite,
#CommentForm_email,
.CommentForm_cite label,
.CommentForm_email label{
display: none;
}

Keep in mind, I'm using this on a protected page, so all visitors will be logged in.

You could add some logic to check if the user was logged in or not, and show the name/email fields to guests.

You'd probably want to hide the fields and labels via JS instead of CSS in that case.

  • Like 2
Link to comment
Share on other sites

  • 3 years later...

Hi Reiska,

I built the Comments form according to your post because i will no reloading pages. I have checked with alert window whether all fields correctly sent with POST. But I do not get the success message in the template and the comment will not be saved. When I run the form without jquery. $.post from HTML it works. Do you have an idea what's wrong?
Link to comment
Share on other sites

  • 3 months later...
I'm making a page-based comments module which will have the same sort of flexibility as the rest of ProcessWire, though it'll be a bit before I've got it finished (and some projects have to come first). But this page-based comments module should support nearly anything you could want to do with it, without having to modify its core code.

Just out of curiosity, did you finish the page-based comments module? It sounds really interesting.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...