extension override in joomla 4

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Locked
rashgang
Joomla! Intern
Joomla! Intern
Posts: 64
Joined: Wed Dec 02, 2009 6:46 am

extension override in joomla 4

Post by rashgang » Wed Aug 18, 2021 4:10 pm

Is there any option given in joomla 4 for override the any component or module file.
Last edited by imanickam on Fri Oct 01, 2021 7:22 am, edited 1 time in total.
Reason: Moved topic » from General Questions/New to Joomla! 4.x to Joomla! 4.x Coding

User avatar
ceford
Joomla! Hero
Joomla! Hero
Posts: 2677
Joined: Mon Feb 24, 2014 10:38 pm
Location: Edinburgh, Scotland
Contact:

Re: extension override in joomla 4

Post by ceford » Thu Aug 19, 2021 6:32 am

You can often override component or module layouts to customise appearance. Look in the layouts folder for examples. And in template html and css folders.

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2908
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: extension override in joomla 4

Post by SharkyKZ » Thu Aug 19, 2021 7:00 am

Extensions that use services can have their services overridden using onBeforeExtensionBoot/onAfterExtensionBoot events. This is supported by components, modules and plugins. However, core modules and plugins haven't been converted to support this. Another option is to use some autoloader hacks but it's not recommended.

rashgang
Joomla! Intern
Joomla! Intern
Posts: 64
Joined: Wed Dec 02, 2009 6:46 am

Re: extension override in joomla 4

Post by rashgang » Thu Sep 16, 2021 5:00 pm

@SharkyKZ could you show me some examples it might helpful for me to override any extension

User avatar
Webdongle
Joomla! Master
Joomla! Master
Posts: 44093
Joined: Sat Apr 05, 2008 9:58 pm

Re: extension override in joomla 4

Post by Webdongle » Thu Sep 16, 2021 5:26 pm

You can use the Template manager to override Components, Modules and Plugins in J4 just like J3
http://www.weblinksonline.co.uk/
https://www.weblinksonline.co.uk/updating-joomla.html
"When I'm right no one remembers but when I'm wrong no one forgets".

rashgang
Joomla! Intern
Joomla! Intern
Posts: 64
Joined: Wed Dec 02, 2009 6:46 am

Re: extension override in joomla 4

Post by rashgang » Thu Sep 16, 2021 5:49 pm

I woud like to override extension model or controller file. How to do it.

User avatar
pe7er
Joomla! Master
Joomla! Master
Posts: 24986
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, Netherlands
Contact:

Re: extension override in joomla 4

Post by pe7er » Thu Sep 16, 2021 7:36 pm

rashgang wrote:
Thu Sep 16, 2021 5:49 pm
I woud like to override extension model or controller file. How to do it.
Joomla does not have overrides for extension Models or Controllers.
However, Joomla will load Model- and Controller classes only once and you can use that behavior:
You can create a system plugin that loads your own class "override" before Joomla loads the original class.

Copy the Model or Controller that you want to override to your plugin,
make your changes in that copy
and let your plugin load that file before Joomla does.

Make sure that you only load your own "override" when needed.
So when the site/admin + option + view + etc are not where you need to run your plugin, "return false" to exit the plugin.
Kind Regards,
Peter Martin, Global Moderator
Company website: https://db8.nl/en/ - Joomla specialist, Nijmegen, Netherlands
The best website: https://the-best-website.com

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2908
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: extension override in joomla 4

Post by SharkyKZ » Fri Sep 17, 2021 6:14 am

rashgang wrote:
Thu Sep 16, 2021 5:00 pm
@SharkyKZ could you show me some examples it might helpful for me to override any extension
Basic code for the plugin:

Code: Select all

public function onAfterExtensionBoot(\Joomla\Event\EventInterface $event)
{
	// Test that this is a component.
	if ($event->getArgument('type') !== 'Joomla\\CMS\\Extension\\ComponentInterface')
	{
		return;
	}

	// Test that this is com_content component.
	if ($event->getArgument('extensionName') !== 'content')
	{
		return;
	}

	// Get the container.
	$container = $event->getArgument('container');

	if (!($container instanceof \Joomla\DI\Container))
	{
		return;
	}

	// Service key to override.
	$class = 'Joomla\\CMS\\MVC\\Factory\\MVCFactoryInterface';

	// Service key not used or can't be overridden.
	if (!$container->has($class) || $container->isProtected($class))
	{
		return;
	}

	// Register the custom MVC factory.
	$container->set($class, new MyMVCFactory);
}
Inside the MyMVCFactory you would add logic to load your class for a given model/controller/view name. It's best that you inspect Joomla\CMS\MVC\Factory\MVCFactory class to see how standard MVC factory works.

Unlike with J3 method mentioned by Peter, your class names don't have to and should not match the overridden class names. And you don't have to copy entire class into your own class. Instead you can extend the class and override only specific methods or class properties.

User avatar
pe7er
Joomla! Master
Joomla! Master
Posts: 24986
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, Netherlands
Contact:

Re: extension override in joomla 4

Post by pe7er » Fri Sep 17, 2021 7:04 am

SharkyKZ wrote:
Fri Sep 17, 2021 6:14 am
Unlike with J3 method mentioned by Peter, your class names don't have to and should not match the overridden class names. And you don't have to copy entire class into your own class. Instead you can extend the class and override only specific methods or class properties.
Thanks for that information SharkyKZ!
I'm developing a new J4 component + plugin, and this is very useful info!
Kind Regards,
Peter Martin, Global Moderator
Company website: https://db8.nl/en/ - Joomla specialist, Nijmegen, Netherlands
The best website: https://the-best-website.com

biirc
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 145
Joined: Thu Mar 12, 2009 11:34 am

Re: extension override in joomla 4

Post by biirc » Tue Nov 30, 2021 10:26 am

Hi,

I have the same question i need to override the Content Router (components/com_content/src/Service/Router.php) in my plugin. (post edited to use the eventInterface syntax

Code: Select all

    public function onAfterExtensionBoot(\Joomla\Event\EventInterface $event)
    {
        // Test that this is a component.
        if ($event->getArgument('type') !== 'Joomla\\CMS\\Extension\\ComponentInterface') {
            return;
        }

        // Test that this is com_content component.
        if ($event->getArgument('extensionName') !== 'content') {
            return;
        }

        // Get the container.
        $container = $event->getArgument('container');

        if (!($container instanceof \Joomla\DI\Container))
        {
            return;
        }

        $app = Factory::getApplication();
        $db = Factory::getDbo();
        $router_file_path  = JPATH_PLUGINS . '/system/myplugin/routers/com_' . $extensionName . '/router.php';
        if (file_exists($router_file_path)){
            // Service key to override.
            $class = 'Joomla\\CMS\\Component\\Router\\RouterFactoryInterface';
            $category_class = 'Joomla\\CMS\\Categories\\CategoryFactoryInterface';
            if (!$container->has($class) || $container->isProtected($class))
            {
                return;
            }
            if (!$container->has($category_class) || $container->isProtected($category_class))
            {
                return;
            }
            // Register the custom Router.
            require_once $router_file_path;
            $prev_router = $container->get($class);
            $container->set($class, new Joomla\Component\MyPlugin\Content\Site\Service\MyPluginContentRouter(
                $app,
                $app->getMenu(),
                $container->get($category_class),
                $db
            ));
        }

Best regards,
Stéphane


Locked

Return to “Joomla! 4.x Coding”