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.

Cannot access $Sender data in 'DiscussionExtender' Plugin

I decided to make my request a lot simpler.

In the 'DiscussionExtender' plugin where this method exists:

public function Gdn_Form_BeforeBodyBox_Handler($Sender) {

I cannot get this value:


It returns 'NULL'

That is the case only in this method. Outside of that function it either returns 'Dicsussion' or 'Question'.

How can I make this function return the discussion type?

Thank in advance.


  • R_JR_J Ex-Fanboy Munich Admin

    $sender was the hardest part to understand for me, too. When I started with Vanilla I had an imagination what OOP is all about but I never used it anywhere and looking backwards I would say I didn't really understood it.

    You can change Vanillas behaviour by utilizing the $this->fireEvent('EventName'); which you can find in several place in the Vanilla code. If you find such a line in class.discussioncontroller.php, $this is an instance of the class DiscussionController. If you find it in class.usermodel.php, $this is an instance of the UserModel. And so on...

    This one important thing to understand is: $this is passed to the plugin as $sender.

    When you have a method in a plugin public function discussionModel_beforeSaveDiscussion_handler($sender) {, you can work with $sender as you would inside the DiscussionModel with $this. $sender is the object that fired the event (that's not exactly true, but true enough to be a good start)
    You can access all the methods and properties of that class by using $sender

    In your example, you are looking at "Gdn_Form" which you could find in /library/core/class.form.php. You can find out if this form has already been send back and what the values are. $sender is in this case an instance of class Form and that class has methods like getFormValue or addError if you would like to manually add an error. But it has no "RequestMethod" information in it, that wouldn't make sense here. The Form class is just a helper class which enables developers to make use of HTML forms.

    You need access to the controller. Do you know what MVC means? if not, here is a very, very simplified "explanation". When you type something in your browsers address bar, the dispatcher determines which controller (the "C" in MVC) has to handle this request. The controller works with the model (the "M" in MVC) by getting/setting data in the database and then utilizes a view (the "V" in MVC) in order to present the data to the user.
    There are far better explanations out there in the net!

    But what you need to know is that the controller holds the information you are looking for. But you do not have access to a controller object, because $sender at the place you are talking about is a Form object...

    /library/core/ is your solution. Look at it, look at its description! Yes, it is important. And it is helpful! If you are working with a $sender that doesn't provide a controller object, you need to get it somewhere and class Gdn has the useful method public static function controller (again: look at it and its description). It's a static function so if you like to, you could add that lines to your code inside the plugin:

    $controller = Gdn::controller();
    $requestMethod = $controller->RequestMethod;

    You don't need to create such a variable and directly work with Gdn::controller()->RequestMethod if you prefer.

  • @R_J , this works.

    One problem, though. It returns, three variable depending on request methods:

    Discussion, Question, editdiscussion.

    This means that every time someone edits a discussion the plugin fields are shown. Again, I only want them to show when the discussion type is 'Question'.

    Can I access 'Discussion->Type' from 'Gdn::controller()'?

  • Answered my own question:

    This is how you access 'Discussion->Type' from 'Gdn::controller():

    $controller = Gdn::controller();
    $requestMethod = $controller->Data('Discussion');
    $TheType = $requestMethod->Type;

Sign In or Register to comment.