Hi,
this question is related to this post.
What I want to do is to remove the URL field from the comment form and instead add an email field and a 'private message' checkbox field (to allow users to send a comment that is not visible on the public site but only to me when I'm logged in to the CMS).
I can create the form, no problem. To do so I added the following code to the end of /mysite/code/Page.php
class PageCommentPlus extends Extension {
function extraStatics() {
return array(
'db' => array(
'CommenterEmail' => 'Text',
'Private' => 'Boolean'
)
);
}
}
class PageCommentPlusInterface extends PageCommentInterface {
// the following method is simply copied from /cms/code/sitefeatures/PageCommentInterface.php; added lines are in green, removed lines in red
function PostCommentForm() {
$fields = new FieldSet(
new HiddenField("ParentID", "ParentID", $this->page->ID)
);
$member = Member::currentUser();
if((self::$comments_require_login || self::$comments_require_permission) && $member && $member->FirstName) {
// note this was a ReadonlyField - which displayed the name in a span as well as the hidden field but
// it was not saving correctly. Have changed it to a hidden field. It passes the data correctly but I
// believe the id of the form field is wrong.
$fields->push(new ReadonlyField("NameView", _t('PageCommentInterface.YOURNAME', 'Your name'), $member->getName()));
$fields->push(new HiddenField("Name", "", $member->getName()));
} else {
$fields->push(new TextField("Name", _t('PageCommentInterface.YOURNAME', 'Your name')));
}
// optional commenter URL
//$fields->push(new TextField("CommenterURL", _t('PageCommentInterface.COMMENTERURL', "Your website URL")));
$fields->push(new TextField("CommenterEmail", 'Your email address (will not be published)'));
if(MathSpamProtection::isEnabled()){
$fields->push(new TextField("Math", sprintf(_t('PageCommentInterface.SPAMQUESTION', "Spam protection question: %s"), MathSpamProtection::getMathQuestion())));
}
$fields->push(new TextareaField("Comment", _t('PageCommentInterface.YOURCOMMENT', "Comments")));
$fields->push(new CheckboxField("Private", 'Send this as a private message (your comment will not appear on this website).'));
$form = new PageCommentInterface_Form($this, "PostCommentForm", $fields, new FieldSet(
new FormAction("postcomment", _t('PageCommentInterface.POST', 'Post'))
));
// Set it so the user gets redirected back down to the form upon form fail
$form->setRedirectToFormOnValidationError(true);
// Optional Spam Protection.
if(class_exists('SpamProtectorManager')) {
SpamProtectorManager::update_form($form, null, array('Name', 'CommenterURL', 'Comment'));
self::set_use_ajax_commenting(false);
}
// Shall We use AJAX?
if(self::$use_ajax_commenting) {
Requirements::javascript(THIRDPARTY_DIR . '/behaviour.js');
Requirements::javascript(THIRDPARTY_DIR . '/prototype.js');
Requirements::javascript(THIRDPARTY_DIR . '/scriptaculous/effects.js');
Requirements::javascript(CMS_DIR . '/javascript/PageCommentInterface.js');
}
// Load the data from Session
$form->loadDataFrom(array(
"Name" => Cookie::get("PageCommentInterface_Name"),
"Comment" => Cookie::get("PageCommentInterface_Comment"),
"CommenterURL" => Cookie::get("PageCommentInterface_CommenterURL")
));
return $form;
}
}
The comment form now looks the way it should. When I run /dev/build/?flush=all no fields get added to the database. probably because I haven't added the extension in /mysite/_config.php . But when I add
Object::add_extension('PageComment','PageCommentPlus');
Object::add_extension('PageCommentInterface','PageCommentPlusInterface');
all I get is a PHP error
PHP Fatal Error: Object::add_extension() - Can't find extension class for PageCommentPlus in /path/to/sapphire/core/Object.php on line 404
I then tried to copy class PageCommentPlus extends Extension (see above) into the Object.php file that's mentioned in the error message but that doesn't work either, because then it says
PHP Fatal Error: Class 'Extension' not found in /path/to/sapphire/core/Object.php
I don't think that I'm too far from a solution. Any hints are much appreciated.
Cheers!
Anatol