Component Installation and hooks / callbacks

Kevin B Posted in Component Development 3 years ago

Hi guys,

I'd like to have a function fired only once when a component is installed. I have tried a couple of things, but am struggling to get it to work.

What I have tried:

In the ossn_com.php I added the following code:

 function webchat_installed() {
    echo "<script>console.log('installed');</script>";
}

ossn_register_callback('component', 'installed', 'webchat_installed');

I thought the callback would fire when the component is installed, but this seems to fire whenever the page loads.
I tried with enabled, installed, disabled, deleted. They all fire together when the page loads and put their respective messages into console - but not when I actually enable / disable the component - only on page load.

I tried looking through the ossn_components class, and it does look as though there isn't a callback triggered on installation anyway - and i'm probably doing completely the wrong thing anyway :(

Any helpful tips?

Replies
gb Kevin B Replied 3 years ago

Oh right, that's excellent thank you :)

Indonesian Arsalan Shah Replied 3 years ago

You wanted to check when component is enabled to see if table exists in database or not? if exists don't create new table?

Add a new file called enable.php in your component folder this will be only triggered once when component is being enabled and enabled.php if you wanted to test after it is enabled.

Callback didn't works in same components that you are going to check as its not loaded yet!

gb Kevin B Replied 3 years ago

Ok, its not actually to output anything, that was just to test if it was triggering. Its just so that on first installation of the component, it checks to see whether certain tables exist in the database.

If they don't it creates them.

Indonesian Arsalan Shah Replied 3 years ago

First of all you should not output these scripts in callbacks of component install, delete, enabled etc.
If you let me know ow what you actually wanted to achieve then I may can help you!