20

Sometimes there is a need to override core classes to get more functionality or to make some core functions work in another way. Is there any way how to override core classes? If it is possible then what issues I can face by overriding them?

2

4 Answers 4

16

Yes, it is possible.

You can use a system plugin to override the classes, please read this and this.

Two problems that you will face:

  1. you need to override the whole class (not just parts of it) and

  2. if the core is updated and the class is also updated/changed, you need to update your override (if needed).

1
  • 4
    It would be useful if you could paste the pertinent steps from the links provided, or at least give a description of the links, in case they move or change. Apr 25, 2014 at 13:48
10

I created a plugin a while ago that allows you to override core classes, but it still falls prey to the problems that @jackJoe pointed out in his answer. However, it allows you to do the overrides cleanly.

https://gist.github.com/dongilbert/3237387

2
  • 7
    Just want to say that this solution works beautifully, and that Don is a true kindred spirit of Open Source - hats off to him and his work!!!
    – GDP
    Jun 18, 2014 at 15:58
  • There is a substantial problem with your simple script, is that it doesn't preload components. So, if you override components, it really doesn't work for most of them (e.g. the com_content component). It works well, but it must preload component so that it can override them (you can't override something that hasn't been declared yet). Oh, and you must use onAfterRoute instead of doing the whole thing in the constructor. If you want to know what I mean by this comment then try overriding components/com_content/models/articles.php .
    – itoctopus
    Nov 6, 2016 at 4:11
4

I was on this one for very long time and promised Don that once I am done will give more details about it .

I needed to override Joomla MVC for components and tell Joomla to look for overrides in my plugin html folder also , not only inside template html folder.

What I never liked is when developers actually take Joomla default class and mess with it , so I rather extend that class if I can, instead of braking something.

My process does following.

  1. Get the class I need to override
  2. Read it, replace class name , create new file in my temp class folder
  3. Load my version of the class
  4. Load Joomla class
  5. Register my extend method which extends function loadTemplate to look for override in my plugin,than joomla template and last joomla component

here is the image rather http://prntscr.com/3d9syq

First parts in the snippet below is checking where the user is , if he is about to update Joomla I am deleting the temp class files that were created to make sure that we get fresh ones when the update is complete which than takes care of JackJoe number 2.

if (($this->Input('option') == 'com_joomlaupdate' && $this->Input('task') == 'update.install') || ($this->Input('option') == 'com_joomlaupdate' && $this->Input('layout') == 'complete')) {

    JFolder::delete(JPATH_SITE . '/plugins/system/yjsg/includes/yjsgcore/classes/extend/classes/');
    JFolder::create(JPATH_SITE . '/plugins/system/yjsg/includes/yjsgcore/classes/extend/classes/');
    $indexContent = '';
    JFile::write(JPATH_SITE . '/plugins/system/yjsg/includes/yjsgcore/classes/extend/classes/index.html', $indexContent);
}

$YjsgJViewDefaultRead         = JPATH_LIBRARIES . '/legacy/view/legacy.php';
$YjsgJViewDefaultFile         = YJSGEXTEND . "classes" . YJDS . "Yjsg" . $isView . "Default" . $IsJversion . ".php";



    //extend JView library class

    if (!JFile::exists($YjsgJViewDefaultFile)) {
        $YjsgJViewDefault = JFile::read($YjsgJViewDefaultRead);
        $YjsgJViewDefault = str_replace('class ' . $isView, 'class Yjsg' . $isView . 'Default', $YjsgJViewDefault);
        JFile::write($YjsgJViewDefaultFile, $YjsgJViewDefault);
    }

    require_once($YjsgJViewDefaultFile);
    jimport('joomla.application.component.view');
    JLoader::register($isView, YJSGEXTEND . $IsJversion . '/component/view.php', true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.