Override Joomla core or 3rd party classes
Override Joomla core or 3rd party classes without editing the core ? Sounds like great right ? Yes there is already solution for this in Joomla. Yep Joomla is always smart you can simply create a System plugin with Overrides class name and load the entire class from your custom path.
We already know the Joomla templates override working pattern simply just create an html folder in your template folder and override the layouts you want.
Here also you can create some custom path something like below .
/templates/your-template_name/code/component_needstooverride/path/file.php
Here I will explain How to override Joomla core com_content class .
class plgSystemComContentOverride extends JPlugin {
public function __construct(&$subject, $config = array()) {
parent::__construct($subject, $config);
}
public function onAfterRoute() {
$app = JFactory::getApplication();
if('com_content' == JRequest::getCMD('option') && !$app->isAdmin()) {
$template = $app->getTemplate();
require_once(JPATH_SITE.'/templates/'.$template.'/code/com_content/controller.php');
}
}
}
These override have few limitations you can’t override a single function from the class. you have to copy entire class file and add your custom code set the path.
For example I will created a plugin to override Virtuemart cart.php helper file. the code of the plugin will look like.
defined( '_JEXEC' ) or die( 'Restricted access' );
if(!defined('DS')) define('DS', DIRECTORY_SEPARATOR);
jimport('joomla.application.component.helper');
class plgSystemComVirtuemartOverride extends JPlugin{
public function __construct(&$subject, $config = array()) {
parent::__construct($subject, $config);
}
public function onAfterRoute () {
$app = JFactory::getApplication();
if('com_virtuemart' == JRequest::getCMD('option') && !$app->isAdmin()) {
$template = $app->getTemplate();
require_once(JPATH_SITE.'/templates/'.$template.'/code/com_virtuemart/helpers/cart.php');
}
}
}
As per the above code just copy the cart.php helper file to your template / code / com_virtuemart / helpers/
In some waste scenario you may face “Cannot redeclare class error” the only situation is some other plugin already loaded same class . so make sure you did a check list once you face such a situations.
Hope its really helps to fix the core editing. A sample Virtuemart cart override plugin can be download below.
One thought on “Override Joomla core or 3rd party classes”
Good work. thanks lol.