Jump to content

Own markup for comment form


thomasaull
 Share

Recommended Posts

It's explained in the documentation. The comment fieldtype returns an array over which you can iterate with foreach. The relevant part is the echo part in the code example given under the headline "OPTIONAL: Generating your own output".

In short: In the example three variables ($cite, $text, $date) are populated with their respective values from the field and than assembled to an individual output.

Hope that makes sense and apologies for my bad English on this technical matters ...

Link to comment
Share on other sites

I don’t have a custom example, but it’s always a good idea to check out the source code of the module itself. You’ll find the render() and processInput() functions particularly interesting.

In the end though, you can pretty much do anything you want, as long as the name-attributes of your inputs match the ones processInput() expects. So if you want more control than the options array gives you, you may just write static mark up into your template file, or you may put in the effort and take the fieldtype’s configurable settings into account, depending on your needs.

  • Like 2
Link to comment
Share on other sites

  • 1 month later...

Hi, does anyone have any example code of the correct way to go about showing just the submit button and the textarea for the actual comment? I want to make the comments available only to members, so they will already have to be logged in to posts comments. Therefore I would have no need for the name and e-mail fields to be displayed to the user and instead would want that to be sent automatically behind the scenes.

I have the new comments upgrade installed, but like the OP said, there isn't really any documentation for rendering the actual comment form in a custom way. I know how to customise the actual comments, but the not the form. I've looked through module source code, but I'm still confused.

​It merely says this in the documentation.....

​​If you wanted to generate your own output (rather than use the built in render() methods), you can iterate the $page->comments field:
<?phpforeach($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>";}

You can likewise do the same for the comment form. But I don't want to get too far into this level yet since this is supposed to be a quick guide.

The line in bold is what I would ideally like to see some examples for.

Thanks

Link to comment
Share on other sites

  • 3 weeks later...

@GuruMeditation if you are looking for the simplest possible solution, your best bet is to hide the fields you don't want displayed to users with CSS:

.CommentFormCite,
.CommentFormEmail {
  display: none; 
}

If you want to go further than that, then you might be better off copying the comments modules to your /site/modules/ and modifying them as you see fit. The comments form is isolated to its own class, called CommentsForm, which you can extend or modify directly. 

In order to only display the comments form if the user is logged in, you would just wrap the renderForm() call around a conditional that checks if the user is logged in.

// render comments list
echo $page->comments->render(); 

// render comments form only if user is logged in
if($user->isLoggedin()) echo $page->comments->renderForm();
  • Like 3
Link to comment
Share on other sites

@GuruMeditation if you are looking for the simplest possible solution, your best bet is to hide the fields you don't want displayed to users with CSS:

.CommentFormCite,
.CommentFormEmail {
  display: none; 
}

If you want to go further than that, then you might be better off copying the comments modules to your /site/modules/ and modifying them as you see fit. The comments form is isolated to its own class, called CommentsForm, which you can extend or modify directly. 

In order to only display the comments form if the user is logged in, you would just wrap the renderForm() call around a conditional that checks if the user is logged in.

// render comments list
echo $page->comments->render(); 

// render comments form only if user is logged in
if($user->isLoggedin()) echo $page->comments->renderForm();

Thanks, I think I will go down the route of copying the comments module and editing the CommentsForm. I'm a bit confused about how it works in terms of modules though. Do I uninstall the Comments module from the /wire/modules/ folder and then copy it to the /site/modules/ folder? If so, how do updates work when you personally release a new version of the comments module?

Thanks again

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...