Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Comment Body Modification

edited April 2011 in Vanilla 2.0 - 2.8
I was having trouble modifying comments via a plugin I am working on, so I decided to modify the source code and see if my code was just wrong or if there was something wrong in my plugin setup.

By putting my code in the source file, I am able to modify a comment before it is saved by adding this line:

$Fields['Body'] = $Fields['Body'] . " Hello world!";

As line 489 of the class.commentmodel.php file. Basically, it just appends a bit of text to the end of a comment, no big deal.

Unfortunately, I cannot get this to work from my plugin. The entirety of my default.php plugin file is below:

<?php // Define the plugin: $PluginInfo['DiceRoller'] = array( 'Name' => 'Dice Roller', 'Description' => 'Dice Rolling Plugin for Play By Post RPGs.', 'Version' => '0.1', 'Author' => "John Hesch", 'AuthorEmail' => '', 'AuthorUrl' => '' ); class DiceRollerPlugin implements Gdn_Plugin{ public function CommentModel_BeforeSaveComment_Handler (&$Sender){ $Sender->$Fields['Body'] = $Sender->$Fields['Body'] . " Hello world!"; } }

As I mentioned, the comment is not modified properly when the code is placed in the plugin file.

I'm trying to figure out where to go from here. Any thoughts?

My initial thoughts are:
1. &$Sender is unable to be modified via a plugin
2. There is some additional setup for the plugin required that I did not notice (it shows up in the plugin manager and it is enabled, though)
3. My code is wrong. Maybe I should reference the body in a different way.

Any help would be greatly appreciated.



  • Options
    LincLinc Detroit Admin
    You have an extra $ in there. $Sender->Fields['Body'], not $Sender->$Fields['Body'].
  • Options
    LincLinc Detroit Admin
    Also a minor quibble but (&$Sender) is redundant (though you'll see it in core code). The $Sender will still be passed by reference regardless of whether the & is present.
  • Options
    So, I changed the public function like you mentioned to:

    public function CommentModel_BeforeSaveComment_Handler ($Sender){ $Sender->Fields['Body'] = $Sender->Fields['Body'] . " Hello world! "; }

    But it still does not modify the comment.
  • Options
    LincLinc Detroit Admin
    edited April 2011
    Went and looked in the CommentModel. 'Fields' isn't in local scope so you can't access it directly AND Fields hasn't been set at that point in the script. Try this:

    $Sender->EventArguments['FormPostValues']['Body'] .= ' Hello';

    It's possible it's $Sender->EventArguments['FormPostValues']->Body instead, sorry a bit rusty at the moment.
  • Options
    Hmm... No luck there, either.

    I'm wondering if the plugin is actually being read... Even when it's enabled, I can't get anything to happen. For example, I tried throwing an exception, but nothing happened.

    Is there more to setting up a Plugin than:
    1. Creating a folder (DiceRoller, in this case)
    2. Creating a default.php file with code (see above for my code)
    3. Uploading these into the /plugins directory
    4. Enabling the plugin on the dashboard?

  • Options
    Okay, the plugin is working now. Thanks so much!

    For posterity, I had to change:

    class diceRoller implements Gdn_Plugin {


    class diceRoller extends Gdn_Plugin {

    Before Vanilla would actually process my code. I'm not sure what the difference is, so I can't give a great explanation about why this is the case.

    It would have been nice had Vanilla given an error or something, but I wasn't getting anything.

    Anyway, Lincoln, the first suggestion you provided:


    Was right on as far as how to access the comment body.

    I'm going to clean the code up a bit and try to figure out how to publish the plugin on this site.
  • Options
    UnderDogUnderDog MVP
    edited April 2011
    I'm going to clean the code up a bit and try to figure out how to publish the plugin on this site.
    @ToastyFish : It's a simple trick to add an addon to the addons site.

    There was an error rendering this rich post.

  • Options
    Yeah, that was totally simple to upload.

    Plugin live at:
Sign In or Register to comment.