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    [label style=”3″]html[/label] folder in your template folder and override the layouts you want.

Here also you can create some custom path something like below .

[code style=”1″]

/templates/your-template_name/code/component_needstooverride/path/file.php

[/code]

Here I will explain How to override Joomla core com_content class .

[code style=”1″]

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’);
}
}
}

[/code]

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.

[code style=”1″]

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’);

}
}

}

[/code]

 

As per the above code just copy the cart.php helper file to your template / code / com_virtuemart / helpers/

[warning]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.[/warning]

Hope its really helps to fix the core editing. A sample Virtuemart cart override plugin can be download below.

[wpdm_file id=33]

 

 

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *