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.
Options

Hooking to rendering of private messages.

edited April 2010 in Vanilla 2.0 - 2.8
I plugged in an event I needed (See: http://github.com/Dykam/Garden/commit/46a5d7539cc8eb8f043f37bd59883a31a046ec6c ), for smiley rendering and some other stuff. I tried to find the same spot in the Conversation app, and did, but I was unable to find the place to fetch the message from. Had to add $this->FireEvent("BeforeMessageRender") somewhere, but the local $Message was not exposed as it was not part of the sender. How do I plug this in without doing more than only additional hooking in the core, like I did with Vanilla.

Comments

  • Options
    Bump...
  • Options
    MarkMark Vanilla Staff
    If you look at line 7 of that file, you'll see this:

    $Sender->EventArguments['Comment'] = &$Comment;

    So, within your plugin's method, you can get at the comment to alter it like this:
    $Comment = &$Sender->EventArguments['Comment'];
    $Comment->Body = YourFunctionToAlterTheBodySomeHow($Comment->Body);
    That should get you where you need to go. Notice the ampersand When I assign the $Comment variable - this makes it assign by reference - which means that the actual comment outside your hook is affected by the change.
  • Options
    oliverraduneroliverraduner Contributing to Vanilla since 2010 Switzerland ✭✭
    edited June 2010
    @Mark When I want to alter a Comment using a Plugin, is it the right way to do it like this?
    public function DiscussionController_CommentBody_Handler(&$Sender)
    {
    $Comment = $Sender->EventArguments['Comment'];
    $Comment->Body = $this->MyModifications($Comment->Body);
    }
    ?
  • Options
    Currently I am using, not sure if all are effective.

    public function Apply($text) {
    // stuff here
    return $text;
    }

    public function DiscussionController_BeforeCommentBody_Handler(&$Sender) {
    $Sender->CurrentComment->Body = $this->Apply($Sender->CurrentComment->Body);
    }

    /**
    * PostController_BeforeCommentBody_Handler
    * @param PostController $Sender
    */
    public function PostController_BeforeCommentBody_Handler(&$Sender) {
    $Sender->CurrentComment->Body = $this->Apply($Sender->CurrentComment->Body);
    }

    /**
    * PostController_BeforeDiscussionRender_Handler
    * @param PostController $Sender
    */
    public function PostController_BeforeDiscussionRender_Handler(&$Sender) {
    if ($Sender->View == 'preview') {
    $Sender->Comment->Body = $this->Apply($Sender->Comment->Body);
    }
    }

    /**
    * PostController_BeforeCommentRender_Handler
    * @param PostController $Sender
    */
    public function PostController_BeforeCommentRender_Handler(&$Sender) {
    if ($Sender->View == 'preview') {
    $Sender->Comment->Body = $this->Apply($Sender->Comment->Body);
    }
    }
  • Options
    oliverraduneroliverraduner Contributing to Vanilla since 2010 Switzerland ✭✭
    @Dykam Thank you for your code examples. Unfortunately for me this did not work... :-(

    Here is a simply example I use to test the altering of the Output of existing comments:
    public function AlteringTest($Text)
    {
    return $Text.' ===== Hello World! =====';
    }

    public function DiscussionController_Render_BeforeCommentBody(&$Sender)
    {
    $Sender->CurrentComment->Body = $this->AlteringTest($Sender->CurrentComment->Body);
    }
    I can successfully activate my plugin, but simply nothing happens with the comments when displaying a discussion... Any ideas?
  • Options
    edited June 2010
    In applications/vanilla/views/discussion/helper_functions.php
          </div>
    <div class="Message"><?php echo Gdn_Format::To($Object->Body, $Object->Format); ?></div>
    To
          </div>
    <?php $Sender->FireEvent('BeforeCommentBody'); ?>
    <div class="Message"><?php echo Gdn_Format::To($Object->Body, $Object->Format); ?></div>
  • Options
    @ the vanilla team. It's severely broken to me, I cannot access the comment anymore.

    On I sidenote, I have the feeling accessing stuff like this is overly complex, but that's just me.
  • Options
    TimTim Operations Vanilla Staff
    @oliverraduner you missed out an important piece of mark's example:

    public function DiscussionController_CommentBody_Handler(&$Sender)
    {
    $Comment = &$Sender->EventArguments['Comment'];
    $Comment->Body = $this->MyModifications($Comment->Body);
    }
    This symbol makes your $Comment variable a reference to the actual comment on the $Sender object, so that any modifications you do to $Comment are reflected in $Sender->EventArguments['Comment'].

    Vanilla Forums COO [GitHub, Twitter, About.me]

  • Options
    TimTim Operations Vanilla Staff
    @Dykam I just committed a change to helper_functions and added a nice event near the top of the file, after all the EventArguments are populated. You should have full control of the comment data now.

    Vanilla Forums COO [GitHub, Twitter, About.me]

  • Options
    oliverraduneroliverraduner Contributing to Vanilla since 2010 Switzerland ✭✭
    Thank you pointing this out, @Tim. However, for me it does still not work - my modifications do not get added to the Comment output...
Sign In or Register to comment.