10 071 Reuse Custom Code
Amigo edited this page 2019-10-04 09:20:49 +02:00


How To Use Code Across JCB By Taking Advantages Of A Few Features

00:00:00 (Click on these time links to see Youtube video)

I would like to show you how to use code across JCB in a very inherent kind of way by taking the advantages of a few features that JCB has that most people may not know is there, or maybe they do but they have not seen it as an advantage. Recently there came up a question on the forum By Alex Dings, 00:00:33, he said that he has admin views but generic code that runs in multiple admin views and then some of it runs only in specific admin views. How I could speed up the maintenance of this kind of thing. His first question had been: Is the Helper Admin class the best way to put this code? Is there a way to keep the generic admin forms functions I wrote separately from these specific ones? Some of you might even be listening and do not understand exactly what we are trying to do.

Pull Up A Docker Image Of Joomla


I am going to pull up a docker image of Joomla. I have a docker file, and quickly going to pull it up. We are going to go and use it. I have it running. Its default is user. I'm going to go to extensions and install. Add the web tab JED 00:01:46 JED Then type jcb and underneath we have a description, you can look at that and install. Getting JCB install from GitHub. We're done. Go Joomla Component Builder, we're almost there. Then 00:02:22 you could grab a demo component. If you don't know how to get access to these components, follow this 'free keys' link and follow the tutorials. I'm going to get the JCB Get Package. The key that you have received, you can add it here. Maybe Force Local Update. It is always good to have all your fields updated. 00:02:52 Continue, within moments we have JCB installed on JCB.

Looking At Examples - GetViewID


Now we can start looking at the examples. That is really what I wanted to get to because the question is being answered by the demo data already. In JCB there we are already doing a lot of this multi inheritance structure kind of relationship. The best way to demonstrate this is to look at JCB itself. We have JCB and then go to Custom Code, we will see there is a whole lot of custom code. 00:03:40 Many of them is this Hash (automation). We want to look at this JCB( manual) option. For example, let's look at the 'getViewID' which is a custom code, that is its name. This is how you use it. We are having a view 00:04:01 called Admin View that is using it. If you click on that it is going to open it for us the Admin View. When we get here, we will see there is the code. I am using this specific getViewID all across my components. It's also being used in JCB. It's powerful. 00:04:31 That is one way to use a specific snippet which you add by using the JCB manual. Give the function a name. You could pass values to the function(see video). Please read the explanation of how that is done. At the end of the day 00:04:55 you can adapt the value structure, for example, if this value(vdm) needs to be changed from instance to instance, you can pass it by value1, that is arg0. You can replace this vdm with [[[[arg0]]]. You must remember to pass, at least value1 00:05:27 to update the [[[[arg0]]] value wherever you are using it. That is one way to use the reuse of the scripting.

Another Way To Reuse - getFilePath


The other way is the one that actually gets used in the helper class. I have one called getFilePath. The getFilePath is also a function I am using across my components. It is being included in JCB. Many of my components are using getFilePath and it is a little function I can pass a few parameters to and can consistently give me back a path of where I want to place or have files from. Now in the PHP helper there is a function and in the component itself, there is a function called 00:06:20'Add PHP(Helper classes - Both admin & site)'. Anything that you put in will not be only added to the Admin Helper Class, but also to the Site Helper Class, a way to avoid doing it twice. There is one to put it in the Admin. You could click 'Yes' and target the Admin Helper or you could target the Site Class Helper. In this case I want most of this code to be everywhere. 00:06:52 If we were to go down to the bottom is the getFilePath. It is forming part of a whole bunch of code that I am adding to the Helper Class of this component. I am also using other components. I do not need to redo this code anytime soon. I do not need to come and open the 00:07:20 component when I want to change the code. I can change the code right there in the place that is in the custom area. If we close out and go back to the Custom Code, you can see that the function is being used in Component Builder and in the php_helper_both. It is where it is added. 00:07:45 If you would need to improve the code, you can always at the bottom see in which areas it is being used. So that is the option for multiple reuse of custom scripting. You could put it anywhere you like in JCB, either in the Component Helper Class or in the component view, php areas. By doing so it gets reused. It gets placed into that code on compilation. That is the answer I suppose, to the power of the Custom Code area.

I realize it could be confusing because we are mixing it with the IDE custom code which is 00:08:28 this 'Hash' (automation). It's where you edit your code in the IDE as I did here. The JCB is keeping track of it and it is saying this is for the Admin Views template default body php file. On this starting Hash value, we are doing an insert of this code and you cannot maintain the code here. But it's only for one file, one location, one place. 00:08:57 Then there's the JCB manual and the JCB manual is the implementation, or the mutated implementation of using custom code snippets, this snippet, all across in custom code areas. I'm using the same word custom code for different implementations. That might have been a mistake because you could miss its possible differences. 00:09:25 But there is most certainly a difference, this does not target a file but an area somewhere in that JCB compilation. I hope that helps.