1

Im trying to create a plugin to override a Joomla class, which otherwise is impossible to override. Here are my codes:

comoverride.xml

  <files>
    <filename plugin="comoverride">comoverride.php</filename>
    <filename>comoverride.xml</filename>
    <filename>index.html</filename>
  </files>
  <config>
  </config>
</extension>

(of course i have my information in my tag and the other necessary information)

comoverride.php

<?php



defined( '_JEXEC' ) or die( 'Restricted access' );
if(!defined('DS')) define('DS', DIRECTORY_SEPARATOR);
jimport('joomla.application.component.helper');
class comcontent 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/models/articles.php');

        }
    }  

Im trying to override the articles.php file, where the class "ContentModelArticles" is in it. but it doesn't seem to work at all unfortunately.

Can anyone please test it for me?

1 Answer 1

2

have a look in the next snippets. It works clearly

plugins/system/comoverride/comoverride.xml

<?xml version="1.0" encoding="utf-8"?>
<extension  version="3.4" type="plugin" group="system" method="upgrade">
<name>plg_system_comoverride</name>
<author>JProof</author>
<creationDate>Februar 2009</creationDate>
<copyright>(C) 2010 Romacron. All rights reserved.</copyright>
<license>GNU/GPL</license>
<version>3.0.0</version>
<files>
    <filename plugin="comoverride">comoverride.php</filename>
    <filename>comoverride.xml</filename>
    <filename>index.html</filename>
</files>
<config>
</config>
</extension>

/plugins/system/comoverride/comoverride.php

defined('_JEXEC') or die('Restricted access');
if (!defined('DS')) {
    define('DS', DIRECTORY_SEPARATOR);
}
jimport('joomla.application.component.helper');

class PlgSystemComOverride extends JPlugin
{

    public function __construct(&$subject, $config = array())
    {
        parent::__construct($subject, $config);
    }

    public function  onAfterRoute()
    {
        $app = JFactory::getApplication();
        if ('com_content' === $app->input->getCmd('option') && !$app->isAdmin()) {
            $template = $app->getTemplate();

            require_once(JPATH_SITE . '/templates/' . $template . '/code/com_content/models/articles.php');

        }
    }
}

I hope that helps you

7
  • hello. did you try your code on a demo website? i get this error: Fatal error: Cannot redeclare class ContentModelArticles in /Applications/MAMP/htdocs/quran/components/com_content/models/articles.php on line 680 on the frontpage. i think its because of the featured function or something. can you pleas have a look at it?
    – Peter
    Commented Oct 27, 2015 at 11:53
  • Yes, i had tried it on a blank Joomla installation without modules or other stuff. "Our" Plugin should to be ordered in JBackend on first position or try also the last. If it doesen't work, you should try the JLoader(best way). Yes, i can have a look on it. greetings
    – JProof
    Commented Oct 27, 2015 at 12:05
  • hi again. i went to www.joomla.org and downloaded the latest version. went on my localhost. created a new joomla page. uploaded my plugin. put it on the top of plugins. disabled all default modules. nothing I get the same error. Im not sure what you mean by JLoader. I can even make a video about what im doing. I still get the error. its so weird. Regards. thank you very much for helping.
    – Peter
    Commented Oct 27, 2015 at 14:04
  • Btw what did you write in your custom articles.php? i had the exact same file as the original but i changed few things which i needed.
    – Peter
    Commented Oct 27, 2015 at 14:29
  • Hello Peter, the article.php was exactly the same, I had have created an category and an test-content article. To display it in frontend i had created an menuitem which is calling the getItems(); in frontend. Right now, i will have a look at the movie. By the way, the JLoader mechanism is complex. For the min. knowledge have a look at: docs.joomla.org/Using_own_library_in_your_extensions dongilbert.net/… dongilbert.net/php-autoloading-and-joomla
    – JProof
    Commented Oct 27, 2015 at 14:58

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.