extension override in joomla 4

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

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Post Reply
rashgang
Joomla! Intern
Joomla! Intern
Posts: 62
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! Enthusiast
Joomla! Enthusiast
Posts: 241
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! Ace
Joomla! Ace
Posts: 1988
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: 62
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: 40354
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: 62
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: 23399
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
https://db8.nl - Joomla specialist, Nijmegen, Nederland
The best website: https://the-best-website.com

SharkyKZ
Joomla! Ace
Joomla! Ace
Posts: 1988
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: 23399
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
https://db8.nl - Joomla specialist, Nijmegen, Nederland
The best website: https://the-best-website.com


Post Reply

Return to “Joomla! 4.x Coding”