Jump to:

23459 Posts in 18906 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Add fields to PageComments form?

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

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

Page: 1 2
Go to End
Author Topic: 3829 Views
  • PeterB
    Avatar
    Community Member
    18 Posts

    Add fields to PageComments form? Link to this post

    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

  • timwjohn
    Avatar
    Community Member
    98 Posts

    Re: Add fields to PageComments form? Link to this post

    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?

  • timwjohn
    Avatar
    Community Member
    98 Posts

    Re: Add fields to PageComments form? Link to this post

    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!

  • Mo
    Avatar
    Community Member
    508 Posts

    Re: Add fields to PageComments form? Link to this post

    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

  • timwjohn
    Avatar
    Community Member
    98 Posts

    Re: Add fields to PageComments form? Link to this post

    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!

  • Mo
    Avatar
    Community Member
    508 Posts

    Re: Add fields to PageComments form? Link to this post

    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

  • timwjohn
    Avatar
    Community Member
    98 Posts

    Re: Add fields to PageComments form? Link to this post

    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.

  • MarijnKampf
    Avatar
    Community Member
    164 Posts

    Re: Add fields to PageComments form? Link to this post

    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');
       }
    }

    3829 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.