Jump to content

Adding additional fields to comments?


ChrisKB
 Share

Recommended Posts

Hello,

First off, ProcessWire looks excellent, thank you for creating this.

I discovered this CMS a couple days ago and I am spending the day learning it today.

I have an important project for a client that requires the feature of allowing surfers to make comments on products. In addition to the normal comment, they will also be able to rate the product in the same comment form. 

Here is an example of what the form may look like:

Name:

Email:

Comment:

Ease of use rating (0 - 5): 

Price rating (0 - 5): 

Product design rating (0 - 5): 

Is this possible to add additional fields like this into comments with ProcessWire?

I would ideally like to use the rating data in other places on the website as well, for example, if ten people give ratings for that product, then I would take the average of all of them and display the average surfer rating on the main page or anywhere.

Any information or a point in the right direction if this is possible would be greatly appreciated!

Thank you,

Chris

Link to comment
Share on other sites

Out of the box, you can't do this with the comments fieldtype included with ProcessWire. Though you could modify it fairly easily to support what you are asking for. It would just be a matter of adding those 3 additional fields to the schema in FieldtypeComments.module, and updating the other files that accompany it to reflect what you need. Before you do this, it may be a good idea to make copies of all the files you need to modify, and keep it as a separate module in /site/modules/FieldtypeCommentsRating/ or something like that. By doing that, you don't risk overwriting your files during upgrades. Let me know if I can provide more direction in getting you started. 

Link to comment
Share on other sites

  • 2 years later...

@ryan I made a copy of the FieldtypeComments module and did as you described. Then I refactored and renamed the class of Comment to CommentRating. I installed the new module in PW. Changed my existing comments field to use CommentRating instead of Comments. That worked fine. But now when I open the page in PW Backend with the template which has the CommentRating field in it I see an error:

( ! ) Catchable fatal error: Argument 1 passed to InputfieldCommentsAdmin::___renderItem() must be an instance of Comment, instance of CommentRating given in W:\wamp\www\beeline\gehaltsprofis\www\wire\modules\Fieldtype\FieldtypeComments\InputfieldCommentsAdmin.module on line 34

Why is FieldtypeComments still used? How can I fix this? Please help. I can provide you with the files if you need them.

EDIT: If I remove FieldtypeComments from wire/modules/Fieldtype it seems to work fine

Link to comment
Share on other sites

@LostKobraKai: Could you explain it different?

I copied the whole FieldtypeComments folder. The InputfieldCommentsAdmin.module is within that folder. I renamed that file to InputfieldCommentsRatingAdmin.module and changed all instances of "Comment" to "CommentRating" like in this excerpt:

class InputfieldCommentsRatingAdmin extends Inputfield implements InputfieldItemList {

	public static function getModuleInfo() {
		return array(
			'title' => __('Comments Admin', __FILE__),
			'version' => 104,
			'summary' => __('Provides an administrative interface for working with comments', __FILE__),
			'permanent' => false, 
			'requires' => 'FieldtypeCommentsRating',
			);
	}
	
	protected $commentIDsToNumbers = array();

	public function init() {
		parent::init();
	}

	protected function ___renderItem(CommentRating $comment, $n) {

		$statuses = array(
			CommentRating::statusApproved => $this->_x('Approved', 'comment-status'),
			CommentRating::statusPending => $this->_x('Pending', 'comment-status'),
			CommentRating::statusSpam => $this->_x('Spam', 'comment-status'),
			);

After that I installed the module "InputfieldCommentsRatingAdmin" in PW Modules section but the error still occurs.

Link to comment
Share on other sites

Thanks for clearing up. There was one line left in FieldtypeCommentsRating.module, which I changed now:

$inputfield = $this->modules->get('InputfieldCommentsRatingAdmin');

Now on to the next problem. Because my custom fields don´t get saved.

Link to comment
Share on other sites

Sure. Thanks for helping me :)

The module is here  

Extract it to site/modules and then install both modules in PW.

Here is the code I use to render the comments and the form on the frontend:

<div class="row">
                    <?php
                    $commentnumber = 0;
                    foreach ($page->commentsrating as $comment) {
                        if ($comment->status < 1) continue; // skip unapproved or spam comments
//                        if ($comment->status < 1 || $commentnumber >= 3) continue; // skip unapproved or spam comments
                        $commentnumber++;
                        $cite = htmlentities($comment->cite); // make sure output is entity encoded
                        $text = htmlentities($comment->text);
                        $title = htmlentities($comment->title);
                        $rating = htmlentities($comment->rating);

                        $date = date('d/m/Y g:ia', $comment->created); // format the date

                        echo "
<div class=\"review\" itemscope itemtype=\"http://schema.org/Review\">";
                        echo "\n\t<div class='rating' itemprop='reviewRating' itemscope itemtype='http://schema.org/Rating'>";
                        echo "
		<span itemprop='ratingValue'>$rating</span><span class='fa fa-star star selected'></span><span class='fa fa-star star selected'></span><span class='fa fa-star star selected'></span><span class='fa fa-star star selected'></span>";
                        echo "\n\t\t<span itemprop='author' itemscope itemtype='http://schema.org/Person'><span itemprop='name'>$comment->cite</span>";
                        echo "\n\t</span>";
                        echo "</div>";
                        echo "<span itemprop='reviewBody'>";
                        echo "<strong class='title'>$title</strong>";
                        echo "<em>$comment->text</em>";
                        echo "</span>";
                        echo "</div>";

                    }

                    ?>
                </div>
                <div class="text-centered">
                    <button class="btn" id="showcomments"><i class="fa fa-comment"></i> Kommentieren</button>
                </div>
                <?php
                echo $page->commentsrating->renderForm(array(
                    'headline' => "<h3>Sag uns deine Meinung</h3>",
                    'successMessage' => "<p class='success'>Danke. Dein Kommentar erscheint, sobald wir ihn freigegeben haben.</p>",
                    'pendingMessage' => "<p class='success'>Danke. Dein Kommentar erscheint, sobald wir ihn freigegeben haben.</p>",
                    'errorMessage' => "<p class='error'>Dein Kommentar konnte nicht gespeichert werden. Bitte überprüfe, ob du alle Felder korrekt ausgefüllt hast, bevor du es erneut probierst.</p>",
                    'processInput' => true,
                    'encoding' => 'UTF-8',
                    'attrs' => array(
                        'id' => 'CommentForm',
                        'action' => './',
                        'method' => 'post',
                        'class' => '',
                        'rows' => 5,
                        'cols' => 50,
                    ),
                    'labels' => array(
                        'cite' => 'Dein Name',
                        'title' => 'Titel',
                        'email' => 'Deine E-Mail',
                        'text' => 'Deine Meinung',
                        'submit' => 'Abschicken',
                    ),
                    // the name of a field that must be set (and have any non-blank value), typically set in Javascript to keep out spammers
                    // to use it, YOU must set this with a <input hidden> field from your own javascript, somewhere in the form
                    'requireSecurityField' => '', // not used by default
                ));
                ?>
  • Like 1
Link to comment
Share on other sites

Did you change anything to the FieldtypeComments.module besides replacing some class names? Neither sleepValue() nor getDatabaseSchema() seem to have any reference to a new rating field, that should get saved.

Edit: If this is your first time working with Fieldtypes I'd suggest looking at the FieldtypeEvent module. It's very well commented to serve as kind of tutorial for creating / modifying these.

  • Like 1
Link to comment
Share on other sites

  • 3 years later...

As I had to add a custom field to FieldtypeComments, I came through this post.

Just in case may be of help for anybody in the future. I prepared a tutorial you can find here:

 

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

×
×
  • Create New...