Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Add fields to PageComments form?


Go to End


9 Posts   5770 Views

Avatar
PeterB

Community Member, 18 Posts

13 August 2009 at 1:04am

Hi,

I need to add an "Email Address" field to the comments form, so it can look like every other commenting system on the web.

Howard Grigg wrote a patch to add this field 14 months ago (http://open.silverstripe.org/ticket/2576) which was turned down in favour of decorating pagecomments (http://open.silverstripe.org/ticket/3093). This ticket is still open and I can't find any docs saying it can be done.

So... is there a way to add an email field, and can anyone provide a code example?

Thanks,
Peter

Avatar
timwjohn

Community Member, 98 Posts

8 October 2009 at 4:25am

Hmm,

I just assumed an email field would be part of the commenting system, but alas... it isn't.

I was really looking forward to implementing gravatars on my site, but I suppose that isn't currently possible...

I then started thinking about extending the PageComment and PageCommentInterface classes to incorporate an email field, but this would be more complicated than I initially thought, cos I'd have to update any reference to either class, I guess.

So, would this mean I'd have to hack the code in the sapphire classes? Don't really like the idea of that.

Does anyone have any information on this, or has anyone successfully added an email field to their comments and comment form?

Avatar
timwjohn

Community Member, 98 Posts

14 October 2009 at 2:23am

Edited: 15/10/2009 1:26am

Well, I've done some searching, and tried to create my own sublass of PageComment and PageCommentInterface but I'm really not having any luck!

I've found threads that touch on the subject, but what I've tried so far just plain isn't working...

[link]http://silverstripe.org/template-questions/show/251237[/link]
[link]http://silverstripe.org/archive/show/7489[/link]
[link]http://silverstripe.org/archive/show/247213[/link]

There are some more threads too, but nothing definitive.

It seems that extending PageComment and PageCommentInterface is like opening a huge can of worms.

This is what I've tried so far:

Extending PageComment to include the new CommenterEmail field:

class PageCommentPlus extends PageComment
{
	static $db = array(
		"CommenterEmail" => "Text"
	);
}

Extending PageCommentInterface plus _Form to handle (or at least I thought) the new field:

class PageCommentPlusInterface extends PageCommentInterface
{
	function forTemplate() 
	{
		return $this->renderWith('PageCommentPlusInterface');
	}
	
	function PostCommentForm() 
	{
		...
		// optional commenter URL
		$fields->push(new TextField("CommenterURL", _t('PageCommentInterface.COMMENTERURL', "Your website URL")));
		$fields->push(new TextField("CommenterEmail", "Your email address"));
		...
	}
	
	function Comments() 
	{
		...
		$comments =  DataObject::get("PageCommentPlus", "ParentID = '" . Convert::raw2sql($this->page->ID) . "' $spamfilter $unmoderatedfilter", "Created DESC", "", $limit);
		...
	}
}

class PageCommentPlusInterface_Form extends PageCommentInterface_Form
{
	function postcomment($data) {
		...
		$comment = Object::create('PageCommentPlus');
		...
	}
}

Altering my Page class to accommodate the new PageCommentPlus class:

class Page extends SiteTree 
{
	...
	public static $has_many = array(
		'Comments' => 'PageCommentPlus'
	);
	...
}

class Page_Controller extends ContentController 
{

	/**
	 * Returns a page comment system
	 */
	function PageComments() 
	{
		if($this->data() && $this->data()->ProvideComments) {
			return new PageCommentPlusInterface($this, 'PageComments', $this->data());
		} else {
			if(isset($_REQUEST['executeForm']) && $_REQUEST['executeForm'] == 'PageComments.PostCommentForm') {
				echo "Comments have been disabled for this page";
				die();
			}
		}
	}

}

I've rebuilt and cleared the old comments out, but now my comment form isn't even showing up...

Has anybody got a nice complete code example of how to successfully add an email field to the comments system? If they could just explain the main steps I'd be vary, very grateful!

Thank you in advance :)

Edit: I edited this down cos this post was pretty huge!

Avatar
Mo

Community Member, 541 Posts

14 October 2009 at 10:50pm

Edited: 14/10/2009 10:51pm

Right, I think you will have to extend the Extension class, then re-write your code a little to accommodat extending the class (Taken from here: http://doc.silverstripe.com/doku.php?id=dataobjectdecorator&s=object%20add%20extension)

First I would do:


class PageCommentPlus extends Extension 
{
	function extraStatics() {
		return array(
			'db' => array(
				'CommenterEmail' => 'Text',
			)
		);
	}
   );
}

Then, for the Interface:


class PageCommentPlusInterface extends Extension
{ 
	public function init() {
		parent::init();
	}
	...
]

Then, in your _config.php file, add the lines:


Object::ad_extension('PageComment','PageCommentPlus');
Object::ad_extension('PageCommentInterface','PageCommentPlusInterface');

I have no idea if this will actually work, but seems more likely than the way you are trying :).

Mo

Avatar
timwjohn

Community Member, 98 Posts

15 October 2009 at 1:22am

Edited: 15/10/2009 4:49am

Thanks for that Mo,

What you're saying makes sense, though there's no way I'd have figured that out myself!

Using this method of extending PageComments is great because I don't need to change anything that refers to that class, and doing the same to PageCommentInterface means (in my understanding) I only need to override that one method PostCommentForm().

I've changed my code, and the good news is that the PageComment table is showing the new CommenterEmail field. There still seems to be a problem with the Interface class however: The email field isn't showing up in the form on my site...

All I did was copy PostCommentForm() to the new class and add the line:

$fields->push(new TextField("CommenterEmail", "Your email address"));

underneath the one that defines the CommenterURL field definition.

Any ideas?

One other thing. Is there any need to define the init() method? why doesn't the extended PageComment class do the same?

Thanks again!

Avatar
Mo

Community Member, 541 Posts

15 October 2009 at 5:03am

This was a bit more complex than I thought. It seems a little hacky to me, if anyone can see a better way, please pipe up :).

You can keep PageCommentPlus the same, but I did notice a typo at the end, so keep an eye out for that.

Next create a new class, like you did the first time:

class PageCommentPlusInterface extends PageCommentInterface {
	
	function PostCommentForm() {
	...
		$fields->push(new TextField("CommenterEmail", _t('PageCommentInterface.COMMENTEREMAIL', "Your email")));
	...	
	}
}
{/code]

Finally, overwrite ContentController::PageComments() in your Page_Controller class like so:

class Page_Controller extends ContentController {

	public function init() {
		parent::init();
		...
	}
	
	/**
	 * Returns a page comment system
	 */
	function PageComments() {
		if($this->data() && $this->data()->ProvideComments) {
			return new PageCommentPlusInterface($this, 'PageComments', $this->data());
		} else {
			if(isset($_REQUEST['executeForm']) && $_REQUEST['executeForm'] == 'PageComments.PostCommentForm') {
				echo "Comments have been disabled for this page";
				die();
			}
		}
	}
}

This new method, calls your PageCommentPlusInterface class, instead of the original PageCommentInterface class. You can now add as many bells and whistles as you like (theoretically at least) in this new class.

Now just run a /dev/build/?flush=all and you should be good to go :)

Mo

Avatar
timwjohn

Community Member, 98 Posts

16 October 2009 at 4:50am

Fantastic! Cheers Mo.

Makes perfect sense now I look at it, but as I said before it would've taken me forever to figure out! I wouldn't exactly call it hacky either. Looks perfectly correct to me.

Avatar
MarijnKampf

Community Member, 176 Posts

18 December 2009 at 10:19pm

Thanks for a very helpful thread!

I've been using the above code to create my own extension to the PageComments. It all works but I've got two issues. I've ended up with two Comment tabs in the CMS, did you have the same issue, or is it something caused by my code?

Also I can't work out how to add the variable I've created to the comment admin system. I thought I could simply add updateFieldLabels(&$labels) function to the Extension definition but this doesn't seem to be doing anything.

class PageCommentMulti extends Extension {
	function extraStatics() {
		return array(
			'db' => array (
				'Location' => 'Varchar(100)',
			),
		);
	}

	function updateFieldLabels(&$labels) {
		$labels['Location'] = _t('PageCommentMulti.Location', 'Location on page');
	}
}

Go to Top