Page 1 of 1
Using core components as a basis for new components
Posted: Tue Oct 09, 2007 12:11 pm
by dodlee
Hello all,
I need to create a component which is similar in functionality to one of the core components, but I want it to have additional features.
My idea was to take the corresponding core component and turn it into a new, installable component, by creating an installation xml file and changing class names, DB table names, etc.
After that, I can start modifying the new component on top of the good basis provided by the original Joomla component.
My question is, whether it's a good idea, or the core components have some special privileges/dependencies which will prevent the new component to function correctly?
Thanks,
Dod
Re: Using core components as a basis for new components
Posted: Tue Oct 09, 2007 12:32 pm
by AmyStephen
Dod -
Thank goodness for the GPL! It liberates the code in this way and allows us to extend what is already there. Not only is this allowed, but it could easily be imagined that the lion's share of extensions on the JED have done so, at least to a certain degree. Ian has called the Joomla! core - a code snippet library - and it is. Hundreds of great code snippets for us to learn from.
Have fun - it's a great environment to play with!
Amy
Re: Using core components as a basis for new components
Posted: Tue Oct 09, 2007 9:45 pm
by bascherz
Replacing (or hacking) core components can be done; I've done it myself. But you leave your creation vulnerable to core updates wiping them out (forcing a reinstall or reapply) or another person doing the same as you and your two extensions not being able to coexist. You'll have to weigh that against the usefulness and potential popularity of your extension. Community Builder is one such extension in that it requires use of a compatible login module, which replaces the one that comes with J! 1.0.x. In that case, however, you cannot use the core login module at all if you want to use CB.
I think in general we should try to use registration and event signaling to the greatest extent possible so that we actually extend or add to the functionality provided by the core rather than resort to replacing parts of it. Just my two cents worth.
Re: Using core components as a basis for new components
Posted: Tue Oct 09, 2007 10:04 pm
by AmyStephen
Bruce's points are excellent. I took the question very differently. I was thinking of using the core weblinks component, for example, as a model for building something new, like a friend's list, or something. In cases where you are adding features, using a core extension as a model can be helpful.
But, if you need a core component to do something slightly different, it's probably not a good idea to "clone" and modify the code. Certainly, it is best to see if the developers have already provided capability for expansion. Joomla! v 1.5 is built that way, unlike v 1.0.x. In some cases, you will find ability to add custom parameters is in place. For example, Johan provided these
two posts for how to customize user parameters and data.
Good points, Bruce! Thanks!
Amy
Re: Using core components as a basis for new components
Posted: Tue Oct 23, 2007 7:27 pm
by TracyDoesPHP
Thanks for the input provided thus far in this thread. However, I'm still not sure about something: if I have a component or module, whether it's natively part of Joomla or is an extension, is there a way to make changes to its functionality without going into the core code for that component/module? In Joomla 1.5, specifically. (I know that you can override the views via the template, but I want to modify actual functionality.)
For instance, I installed a photo gallery component into my 1.5 RC3-based site. The component resizes images to 100x100 for their "thumbnail" view, but I need it to be 160x105. There is a helper class with a method that determines the sizes. I could write an extension class for that helper class, but then, as I see it, I have two options:
1. Give the extension class a new name. Go into the component's code and replace every call to the original class with a call to the extension class. This is clearly bad and not easily maintainable.
2. Rename the original class and give the extension class the name of the original class. This way, I don't have to go into many different places in the component's code, but I still have to make manual changes to that helper class if the component is ever upgraded.
Is there a better way to override some of the functionality of a component or module, similar to the nice way you can override a view in 1.5?
Re: Using core components as a basis for new components
Posted: Wed Oct 24, 2007 9:06 pm
by ianmac
This is possible if the component is designed to allow it...
You could create a factory type class similar to JFactory which is used to retrieve a reference to various objects for your component. Then, you can write plugins that can replace these objects...
i.e. take JDocument for example.
I can do:
$document =& JFactory::getDocument();
This gets me a reference to the current document object.
Now, say I wanted the system to use a custom document object. Then I do:
$document =& JFactory::getDocument();
$document = new myCustomDocumentClass();
Then, whenever any other code calls the JFactory::getDocument() method, it gets the new document object.
Presumably, your myCustomDocumentClass would extend the JDocument class.
This method could be used to create extendable extensions. None of the native components are written that way, but there are elements of the framework that are.
Ian
Re: Using core components as a basis for new components
Posted: Wed Oct 24, 2007 9:07 pm
by ianmac
DISCLAIMER: I have not tried the above method - it is just an idea, so I don't know how well it would actually work in practice.
Ian