Adding Fields to Page Comments
What it Does
Adds an email field to page comments.
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.
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.
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.
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.
(NOTE: I tried to use LeftAndMainDecorator initially instead of extending CommentAdmin, but I ran into several issues)
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.
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.
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
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.