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?
-
1It would be great if you could refine your question to make it a bit more specific or to describe a clear problem.– Valentin DespaCommented Apr 23, 2014 at 16:00
-
For Joomla 3 users - check these. It will help you. joomla.stackexchange.com/questions/5449/… Plugin - github.com/ghazal/plg_mvcoverride Thanks joomla.stackexchange.com/users/270/ghazal– chaCommented Nov 28, 2014 at 11:11
4 Answers
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:
you need to override the whole class (not just parts of it) and
if the core is updated and the class is also updated/changed, you need to update your override (if needed).
-
4It 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. Commented Apr 25, 2014 at 13:48
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.
-
7Just 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!!!– GDPCommented 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
. Commented Nov 6, 2016 at 4:11
For Joomla 3 users - check these. It will help you.
Override MVC classes in Joomla 3
Plugin - https://github.com/ghazal/plg_mvcoverride
http://docs.joomla.org/How_to_override_the_component_mvc_from_the_Joomla!_core
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.
- Get the class I need to override
- Read it, replace class name , create new file in my temp class folder
- Load my version of the class
- Load Joomla class
- 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);