Skip to content

071 Reuse Custom Code

Llewellyn van der Merwe edited this page Dec 19, 2018 · 10 revisions

Would like to Show you how to Use code across JCB Never inherit in kind of way By taking the advantages of a few features that Jay-Z has Features that knows may not know is there or maybe they do but they not seen it's advantage And then recently there actually came up a question On the Forum Hi Alex things are skiing About You know your husband views but Generic code that runs in Multiple admin views and then some of it runs only in specific admin views And how I could how I could speed up the maintenance of this kind of thing So his first question is is the helper at mint class the best way to put this code and then is there a way Keep the direct admin forms functions I wrote separate from these specific ones Ok I'm sure some of your might even be listening here and I'll understand exactly what we trying to do Answer I'm not even sure if I'm exactly going to answer this you know So I'm gonna quit it pull up a Docker image of Joomla I have a doc file there just quickly gonna pull it up And then we gonna go and use it Ok Ok so I've got it of running I think it's default is user Ok so where in here I'm gonna go to extensions and install And Basically at the web tab The JD Kud Then tie her recipe Here we have Description can look at that And install Sort of getting the JCB install from GitHub Ok we're done Go to Mokopane and builder when was there Then We could Grab a demo component If you don't know how to get access to these components just follow this link and Follow their tutorials X gonna get the JCP package And then the key that You will send it will add here Mary Force an update Toe is good So get all your Fields updated Ok now Continue And within moments we have JCB Installed on JCB Now we can start looking at the examples That's really what I wanted to get to Because the question is being answered by the demo data Already So In JCB there we already doing a lot of this multi Inheritance structure kind of relationships And So the best way to sort of demonstrate this is to actually look at a CV itself K almost there Hey I got JCP Here Now if we go to custom code We'll see there's a hole that a customer code And Many of them is this hash Automation Now we wanna look at this JCB manual option For example let's look at the good view ID Which is a custom code Basically that is his name This is how you use it And Here We are having a view Called admin View That is using it So if we click on that He's going to open for us the admin View And when we get here we'll see there is the the code now This actually is the I'm using this specific get view ID All across my components is just It's also being used in JCB But it's it's powerful So I'm using it everywhere So that's one way To use A a specific snippet Would you add here use the JCB manual Give the function and name You could pass values to the function like this And Please read the explanation here To how that is done And then today You can then add up the value structure for example if If this value here needs to change from instance to instance you can pass it by The new one Which that will be R20 And then you can just actually replace this here with 3 Then Park 093 again like that And then you just remember to pass At least one value To update This value Where are we using it That's one way to use the reuse the scripting Now the other way Which is the one that actually gets used in the helper class It's like here I got one called get file path To get file path is also a function Which I am using a cross my components It is by just also being included in JCB But many of my components is using get file path And basically It is a little function That I can pass a few parameters to ne can consistently give me back a path of where I want to place or get far Piles from Now in the PHP helper there is a function And the component itself That's me here There is a function called Helper classes for both admin insight So anything you put in here will be not only added to the admin helper class but also to the site Helper class sort of a way to Avoid doing it twice And As you can see There is one to just put it in the admin So you could just take care and just target the admin helper Or you could just talk at the site class helper But in this case most of this code I wanted to speak everywhere And if we were to Go down to the very bottom suppose Your Is the Get file Path So it is forming part of a whole bunch of code that I'm adding to the helper class of this component And which I'm also using another components so I don't read Do you need to redo this code anytime soon unless in in sorry I don't need to come and open there Component when I want to change this code I can just change the code Right there In the Place that it's in the custom area So that means if we close out here And we go back to the customer code You can see that the function is being used in component builder and in what field there bitch be helper both Sweat added And If you need improving the code you can always at the bottom see in which areas is being used So that is the option for multiple reuse of custom scripting So you could put it anywhere you like in JCB either in the component helper class or in the component View You know PHP areas And by doing so it gets really used it gets placed into that code on compile a And that's really the answer I suppose ^ the customer code area I realise it could be confusing because we are mixing it with the idea customer code which the These are This hash Automation Is basically where you Edited your code in the ID like I did here And the JCP is keeping track of it And it's saying ok this is for the admin views tampered before body php file And on the 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 Then there's the JCB manual and the JCB manual Is the implementation order mutated in implementation Of using custom code snippets this clip it All across in customer code areas So that means I'm using the same word custom code Four different implementation And That might have been a mistake because You could Miss it's possible differences But there is most certainly a difference this doesn't target a file but a actual area Somewhere in that JCB compilation So Yeah I hope that helps Let me know

Clone this wiki locally