0

I'm trying to override a Joomla method (toggleTables in the Indexer class of the com_finder) using a plugin, but it doesn't work. Where am I going wrong?

use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Component\Finder\Administrator\Indexer;


class plgSystemFinder_override extends CMSPlugin
{
    function toggleTables($memory)
    {
            return true;
    }
}
3
  • Can you please provide better feedback than "it doesn't work"? Were you able to break down the problem to isolate the earliest point of failure? were there any errors generated? Did you write any "sanity checks" (basic printing of text to screen) to prove that the method was being executed? Sometimes the smallest clues can help volunteers to confidently provide you an answer faster. Apr 27, 2022 at 22:48
  • Sorry, I thought it was clear. It simply doesn't override and the content of the real method is execute. I understand that it doesn't work because the original method is executed (so, my override is ignored).
    – Nintendomi
    Apr 28, 2022 at 8:30
  • I assume you are trying to override the toggleTables method to try and solve your issue here, joomla.stackexchange.com/questions/32109/… . Looking at the code in J4 anything that calls toggleTables() is passing 'false' so I am not sure overriding it is going to help you with your problem. Do you use and IDE with XDebug or similar to see whether toggleTables is actually being called?
    – Irata
    Apr 29, 2022 at 11:44

1 Answer 1

1

I don't think you have correctly grasped how the Plugin can be used to override a function. In your example there is nothing to connect the function toggleTables with the Indexer class you are trying to override.

I haven't done exactly what you are trying to do so this will require you to do some more reading to get it to work for your circumstances and maybe someone else can provide an exact or working example, however these are the parts you need to have.

To start with your Plugin should be written to register the file where your override code can be found and this is done by registering the library early on in processing, in this case just after things get initialised.

use Joomla\CMS\Plugin\CMSPlugin;

class plgSystemFinder_override extends CMSPlugin
{
    public function onAfterInitialise()
    {
        JLoader::register(location of your version of the toggleTables function);
    }
}

More information here; https://docs.joomla.org/Using_own_library_in_your_extensions/en and https://forum.joomla.org/viewtopic.php?t=984541 to help you decide how you want to find your version of the function.

You also need to install your version of the code for the function into Joomla so the the above plugin can find it. Unless you have some other extension as part of your changes then I would say look at installing it as a Library.

<?xml version="1.0" encoding="utf-8"?>
<extension type="library" method="upgrade" version="4.0">
    <name>My Test library.</name>
    <libraryname>mytest</libraryname>
    <files>
        <folder>Classes</folder>
        <folder>language</folder>
        <filename>mytest.php</filename>
    </files>
</extension>

More information here, https://docs.joomla.org/Manifest_files towards the bottom discusses Libraries. You could also look at Package Manifests to combine the the Plugin and the Library to be installed together.

And finally you need to perform the actual override in your code so you would have something like this in the code you have installed in the Library in the previous step so that your code will be called instead of the core code.

##Updated in response to the comment by @sharky

class Indexer  
{
    entire contents of Joomla\Component\Finder\Administrator\Indexer.php including your changes
}

However this answer is incomplete as I now understand we need to replace, rather than override/extend, the entire Class but I am not sure what needs to be done in the plugin to replace the core class that is already loaded.

7
  • Thank you Irata, I actually didn't understand how method overrides work. I am trying to do what you suggested, but I'm probably doing something wrong because my code is not executing. Below are my codes:
    – Nintendomi
    Apr 28, 2022 at 9:31
  • I installed a library with this code: <?php class Indexer extends Joomla\Component\Finder\Administrator\Indexer { protected function toggleTables($memory) { $db = $this->db; // Set the tokens table to InnoDB. $db->setQuery('ALTER TABLE ' . $db->quoteName('#__finder_tokens') . ' ENGINE = INNODB'); $db->execute(); return true; } }
    – Nintendomi
    Apr 28, 2022 at 9:33
  • And I edited my plugin in this way: <?php use Joomla\CMS\Plugin\CMSPlugin; class plgSystemFinder_override extends CMSPlugin { public function onAfterInitialise() { JLoader::register('Indexer', JPATH_LIBRARIES . '/fixfinder'); } }
    – Nintendomi
    Apr 28, 2022 at 9:35
  • But the database table is not changed when the indexer methos is run in Joomla. So the override doesn't work... Where am I wrong now?
    – Nintendomi
    Apr 28, 2022 at 9:37
  • If you are not using an IDE of some sort to step through the lines of code can you place a "die('Code in override is being run')" to cause the processing to stop when it is run to ensure that your override is being executed or not. Also when you install a Plugin, by default it will be Disabled so make sure it is Enabled. Once we get the code to execute then you can look at whether your DB changes are working.
    – Irata
    Apr 28, 2022 at 9:58

Your Answer

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

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