Home

Adding Fields to Page Comments

What it Does

Adds an email field to page comments.

Requirements

This is based on and tested in Silverstripe 2.3.5 (I believe it will work as far back is 2.3.2, but this is untested)

This isn't as simple as it should be.  I am also sure that things could be done in a better way, please comment if you have improvments.

Override PageComments

You'll need to override the ContentController->PageComments() function.  This is essentially a copy and paste from sapphire/core/control/ContentController.php and change the reference to PageCommentInterface to PageCommentExtendedInterface, which you will create in the next step.
mysite/code/Page.php

Extending PageCommentInterface

PageCommentInterface is the Request Handler for page comments.  You will need to override the function PostCommentForm().  Unfortunately because of the way this function is written calling the parent on it and then adding the new fields is not possible (as far as I know), so you must copy the existing function from the parent class and then add your new field in the fields and then to load the new field from the session you'll also have to add your new field to the loadDataFrom array.  You are not done with this file yet, now you must also set the session for your new field.  To accomplish this you'll have to extend the internal class PageCommentInterface_Form and the function postComment().  In this case you can load up your session and then just call the parent function.  Don't forget to change the reference to PageCommentInterface_Form to your new class.
mysite/code/PageCommentExtendedInterface.php:

Setuping up your site configuration

You will be creating a decorator, you must include some code in your _config.php to assign it.  You will also be creating an extension to an Admin section you will want to add a rule to redirect the comments action to your new extension, also remove the old admin section.
mysite/_config.php:

(NOTE:  I tried to use LeftAndMainDecorator initially instead of extending CommentAdmin, but I ran into several issues)

Decorating PageComment

This could be as simple as just adding a new db static and pushing a new TextField onto CMSFields, however I took it a step further and set it up to send and email after someone posts a comment.  The email needs better formatting and it could use the admin email and the links to delete and mark as spam could be generated more dynamically.  This is not really part of the tutorial but I thought I would share.
mysite/code/PageCommentDecorator.php:

Extending CommentAdmin

And Finally, if you want to see the new data fields in the backend you'll have to extend the CommentAdmin and override EditForm(). Again, I had to copy and paste the function from the source due to the way it was written.  You must add the table field and the PopupFields for your new data field.
mysite/code/CommentAdminExtension:

That is it.  I'd apreciate any feedback you have as this is my first tutorial.  I'm considering packaging this as a module, it would also be my first module.  I feel the code needs to be a bit better for a module so if you have any improvements don't hesitate contacting me.

Special thanks to Mike Mannix for running through this and proving it works and is somewhat easy to follow, as well as fixing my horrible grammar.  Mike is working on his first Silverstripe site elmbrew.com, he completed the level one tutorials and then moved on to this.  It is a work in progress so keep an eye on it.

About the Author

Name: Dan Rye

Website: http://www.ryedesigns.com

Dan is a developer running a small Photography, Design, and Development Studio - Rye Designs with his wife Vicki.  Based in the Cincinnati, OH area, serving the tri-state area and beyond.  Dan is also a Software Engineer for MAG, developing offline manufacturing software for the aeronautics industry.

Comments (5)

  • Excellent, thank you. It worked brilliantly (SilverStripe 2.4) :)

    Posted by Pragmatic Design, 04/07/2010 9:21am (25 days ago)

  • "Great article!

    I did something similar recently and I just wanted to note that there actually is a way to get the form fields from the parent and modify them.

    What you do is something like this
    $form = parent::PostCommentForm();
    $fields = $form->Fields();
    That gives you the fields on the PostCommentForm. After you've added/removed the fields you want simply call
    $form->setFields($fields);
    and you're done.

    This should hopefully simplify extending PostCommentForm if all you want to do is add some extra fields to the comment form. "
    Thank you for updating us.

    Posted by car insurance quotes, 24/05/2010 1:22pm (2 months ago)

  • Thanks for the feedback! I hope it has helped some folks. I'll have to refine it to include your suggestion Marcus.

    Thanks again.

    Posted by Dan Rye, 01/03/2010 1:30pm (5 months ago)

  • This is great stuff. I've just started using SilverStripe and was surprised to see the comments doesn't include an email address as a field by default. I'll be using your tutorial in the coming days to enable this when I change over my blog from its existing CMS to SS.

    Posted by Chris Hope, 12/02/2010 3:14pm (6 months ago)

  • Great article!

    I did something similar recently and I just wanted to note that there actually is a way to get the form fields from the parent and modify them.

    What you do is something like this
    $form = parent::PostCommentForm();
    $fields = $form->Fields();
    That gives you the fields on the PostCommentForm. After you've added/removed the fields you want simply call
    $form->setFields($fields);
    and you're done.

    This should hopefully simplify extending PostCommentForm if all you want to do is add some extra fields to the comment form.

    Posted by Marcus Dalgren, 07/02/2010 3:39pm (6 months ago)

RSS feed for comments on this page RSS feed for all comments

Post your comment