How to capture Vanilla Model Events in custom addon?

Hello,

I want to handle BeforeSaveDiscussion event of DiscussionModel in a custom plugin. So I created a plugin with below code and enabled it.

class MyPlugin extends Gdn_Plugin
{

public function __construct()
{
    parent::__construct();
}

public function DiscussionModel_BeforeSaveDiscussion_Handler($sender, $args)
{
    echo "<pre>";
    die("hello");
}

}

But it looks like this event is not getting called because discussion gets saved but "hello" is not printed. I have checked the browser's console.

I borrowed the above from the QnA plugin (plugins/QnA/class.qna.plugin.php):

public function DiscussionModel_BeforeSaveDiscussion_Handler($Sender, $Args)
{
$Sender->Validation->ApplyRule('Type', 'Required', T('Choose whether you want to ask a question or start a discussion.'));

  $Post =& $Args['FormPostValues'];
  if ($Args['Insert'] && GetValue('Type', $Post) == 'Question') {
     $Post['QnA'] = 'Unanswered';
  }

}

Can anyone please help me out here. I might be missing a very small thing here.

Thanks.

Comments

  • Verify that your plugin actually works first:

    public function base_render_before($sender) {
        echo 'I am alive!';
    }
    

    You should see the text appear above every page.

    My themes: pure | minusbaseline - My plugins: CSSedit | HTMLedit | InfiniteScroll | BirthdayModule | [all] - PM me about customizations

    • Vanilla APP » Learn more «
    • iOS & Android App for Vanilla - White label app for your forum
  • neelkanthneelkanth New
    edited September 2018

    @Bleistivt Thanks for reply. Yes 'I am alive!' is coming on top of page. But how should I check that my

    public function DiscussionModel_BeforeSaveDiscussion_Handler($sender, $args)
    {
        echo "<pre>";
        die("hello");
    }
    

    is getting called or not inside custom plugin.

  • I have tried discussionController_render_before as well but no success.

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    Have you solved it? From time to time I faced similar issues when working with code that I thought could be no problem, because I simply have copied it. But deleting it and typing it by hand always solved that problem and I never investigated why it didn't worked in the first place. Your code above looks good and should work


Sign In or Register to comment.