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

Write a module - how to get Magic Events to work?

judgejjudgej
edited September 2010 in Vanilla 2.0 - 2.8
I am creating a module, and am scratching my head at the fist stage: trying to get magic events to work. The plugin script is being executed, but none of its Magic Events are being inserted into the output flow.

The plugin is called DisplayName and it enables and disables fine. I have a single file so far with a single class and the PluginInfo array:

class FileUploadPlugin extends Gdn_Plugin { public function DisplayNameController_BeforeDiscussionRender_Handler(&$Sender) { echo ' YOU CANNOT SEE ME!! WHY NOT?? '; } // Required implementation of Setup() not shown. }

When I view a discussion I expect to see that text somewhere in the response (I know, the wrong place - but *somewhere* would be nice).

I also took the FileUpload plugin and went the other way - I added the echo statement to its BeforeDiscussionRender_Handler method and then deleted just about everything else from its main class, and the echo displays just as I would expect it to.

I am guessing then that I have missed out something, but cannot see what it is. Should a really minimalistic plugin like this work - just one class, one Magic Event method and nothing else?

Thanks.

Comments

  • Options
    judgejjudgej
    edited September 2010
    Well, wotdyaknow! Just writing it down seems to have given me the solution.

    It looks like my event hanlder had the wrong name. It should be:

    DiscussionController_BeforeDiscussionRender_Handler

    So I am going to guess that there are, or can be, many other BeforeDiscussionRender events that could be added to other controllers. So that I target the right controller I need to specify it, i.e. "Discussion". The documentation - http://vanillaforums.org/page/Plugins - kind of covers this, and it now makes a lot more sense.
  • Options
    TimTim Operations Vanilla Staff
    edited September 2010
    Lol. You basically have it right.

    You made a Handler type method.... Handler suffix means "Event Handler". What comes directly before the suffix is the actual fired event name. If you're trying to handle an event fired like $this->FireEvent('MyEventName'), this part would be "MyEventName".

    So now we have _MyEventName_Handler(). Directly before that comes the "context" of the event, in the form of the classname of the object that fired the event. So since your event is fired in the DiscussionController, you write "DiscussionController".

    DiscussionController_MyEventName_Handler.

    Or in your case, "DiscussionController_BeforeDiscussionRender_Handler".

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

Sign In or Register to comment.