1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How do I extend the class XenForo_Upload

Discussion in 'XenForo Development Discussions' started by AndyB, Sep 25, 2013.

  1. AndyB

    AndyB Well-Known Member

    I'm trying to create an add-on which needs to extend the class XenForo_Upload.

    This class is located in the following file:


    This file executes when an attachment is uploaded.

    The problem is my add-on does not work, I expect the die command to echo "got here".

    Am I listening to the correct event "load_class"?

    Thank you.
  2. AndyB

    AndyB Well-Known Member

    The directories and files

  3. AndyB

    AndyB Well-Known Member



    class Andy_ImageRotate_Listener
       public static function 
    Upload($class, array &$extend)
    $extend[] = 'Andy_ImageRotate_Upload';

  4. AndyB

    AndyB Well-Known Member

    Code Event Listener

  5. AndyB

    AndyB Well-Known Member



    class Andy_ImageRotate_Upload extends XFCP_Andy_ImageRotate_Upload
       protected function 
    "got here" );

  6. Daniel Hood

    Daniel Hood Well-Known Member

    There are some classes that are only called statically and cannot be modified. It seems as if you are attempting to rotate images, if so you may want to just put a button next to the attachments to rotate and do it upon being clicked instead of automatically. I'm sure eventually more code events will be created. It's been brought up a lot lately by developers.
  7. AndyB

    AndyB Well-Known Member

    Thank you for your information, Daniel.

    How can we determine which classes are called statically?
  8. Daniel Hood

    Daniel Hood Well-Known Member

    Well most commonly we just unfortunately stumble upon them. However, you could browse the core files and see where all the code event fires are I guess. Eventually you'll get a hang of how all classes are loaded and understand basically which ones are initiated and which ones are called statically. A short list of the ones off the top of my head that are static are the ones in the root folder (Application, Input, Link, etc) and all the template helpers. Basically load_class just works on the other load_class_* options, it's just more vague.

    for example load_class_datawriter could also be executed through load_class. Evidenced here (/library/XenForo/Application.php):

    public static function resolveDynamicClass($class$type ''$fakeBase false)
    XenForo_CodeEvent::fire('load_class', array($class, &$extend), $class);
            if (
    XenForo_CodeEvent::fire('load_class_' $type, array($class, &$extend), $class);
    Some of these can be customized through init_dependencies and other types, but it's an entirely different process and it's not technically extending.
    Last edited: Sep 25, 2013
    AndyB likes this.
  9. AndyB

    AndyB Well-Known Member

    Upload.php is in the root folder so I suspect it will be called statically. Looks like hacking the Upload.php file directly is the only option for now.

    Thank you for taking the time to explain how the load_class is used.
    Daniel Hood likes this.
  10. Daniel Hood

    Daniel Hood Well-Known Member

    No problem. You should still possibly try a different method first. Hacking a core file is absolute last resort at all times.

    Could you potentially use the controller? Maybe even a DataWriter::_postSave() option? A deferred method? Does it absolutely have to be the XenForo_Upload class?
  11. AndyB

    AndyB Well-Known Member

    I suppose the image could be auto-oriented at some other point, but I haven't looked yet. What I would like to do is create an add-on which performs what this hack does.

        * Checks the state of the upload to determine if it's
        * a valid image.
    protected function _checkImageState()
    // start hack   
    exec("/usr/bin/mogrify -auto-orient $this->_tempFile");
    // end hack 
  12. Marcus

    Marcus Well-Known Member

Share This Page