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.

How to insert an code segment into a function in class.format.php

edited March 2011 in Vanilla 2.0 - 2.8
I need to edit something in class.format.php like function LinksCallBack()
But, it's not good to edit directly core. So, I want to config it by a custom plugin.

Anybody help me, thanks in advance!

Comments

  • Anyone help me?
  • lucluc ✭✭
    Check for Events fired next to where you want to make your changes.
    If one doesn't exist, it could be added into core.
  • LincLinc Admin
    edited March 2011
    You could also overwrite the entire function in a plugin using the function Gdn_Format_LinksCallBack_Create($Sender) { //new code using $Sender-> instead of $this-> }

    Or just let us know what you're trying to do and where you need the event :)
  • Does the {Object}_{Method}_Create() method in a plugin really override a core method like this? Does the core method not need to be prefixed with an "x" or something to move it out of the way first?
  • SS ✭✭
    edited March 2011
    @judgej
    No.
    LinksCallback is
    1) static
    2) protected
    3) Gdn_Format is not pluggable

    May add thus before $Result
    Gdn::PluginManager()->EventArguments['Result'] =& $Result
    Gdn::PluginManager()->FireEvent('FormatLinksCallback');
    But I'm not sure that there is a good solution.
  • LincLinc Admin
    edited March 2011
    Dang. OK, it doesn't work in Gdn_Format because it isn't pluggable as @S said, but generally yes that would work.

    I bet it would override a static/protected method though. Vanilla looks for _Create methods before it looks for a "real" method afaik.

    I've definitely thought about making Gdn_Format pluggable but didn't have a compelling enough argument to do it yet.
  • @Lincoln I thought Vanilla looks for the _Create method only after it fails to find the real method? If the "real" method is already there, then there is no need for Vanilla to start digging through alternatives provided by the plugins, and in fact there is no way it *can* do so, since it needs a method "miss" to start the search elsewhere.
  • LincLinc Admin
    edited March 2011
    Check out the dispatcher around line 280:
    if ($PluginManagerHasReplacementMethod) {
    try {
    Gdn::PluginManager()->CallNewMethod($Controller, $Controller->ControllerName, $ControllerMethod);
    } catch (Exception $Ex) {
    $Controller->RenderException($Ex);
    }
    } else {
    $Args = $this->_ControllerMethodArgs;
    $Count = count($Args);

    try {
    call_user_func_array(array($Controller, $ControllerMethod), $Args);
    } catch (Exception $Ex) {
    $Controller->RenderException($Ex);
    exit();
    }
    }
    It "prefers" the PluginManager's _Create version. I asked Tim about it and he said it's intended and staying that way.
  • judgejjudgej
    edited March 2011
    Ah, okay - that is just for the front-end controller methods. It makes sense, is the right way to do it IMO, and is good to know.

    I was thinking about the more low-level API calls into the models, what with the OP question referring to the underlying supporting API of Vanilla - methods which never touch the dispatcher or are exposed directly to users. I should have been clearer.
  • So really what we'd need to do here is make Gdn_Format extend Gdn_Pluggable AND prefix its methods with an 'x'.
  • SS ✭✭
    Overriding protected method is not possible, because it doesnt returns to collection by get_class_methods() see Gdn_PluginManager::RegisterPlugin()
    static function can be overriden with magic method __callStatic but it is PHP 5.3
Sign In or Register to comment.