Executing code at extension activation

edited July 2007 in Vanilla 1.0 Help
Hey. Where should I place some code I want to execute when my extension is activated/installed? I've written some code to check if a role and a user exists and to create those if they don't exist yet. At the moment I have the code in my default.php and it executes every time, which is a bit stupid :P Thanks in advance :)

Comments

  • You mean you want to make a kinda installation routine for a script? What most people do is add a configuration key (such as EXTENSIONNAME_INSTALLED) when the extension first runs (and does the user creation or whatever), then every time the script runs just use if ($Configuration['EXTENSIONNAME_INSTALLED'] != 1) { do the install routine } There are a couple of extensions out there using that general theory..I'm pretty sure attachments is one of them if you want to check it in more detail.
  • ithcyithcy New
    edited July 2007
    one way to do it, like mini said:

    if (!array_key_exists('YOUR_SPECIAL_EXTENSION_VARIABLE', $Configuration)) { //do your installation stuff here //and when it's all successfully completed, do the next step: AddConfigurationSetting($Context, 'YOUR_SPECIAL_EXTENSION_VARIABLE', 'SOME_VALUE'); }
  • Thanks to both of you! That was just what I needed.
  • It just occured to me that if I needed to do some cleaning up when uninstalling an extension I can't really use code from my extension.. or can I? Is there some hook to run code just before deactivating the extension?
  • There used to be but I think it got taken out cause some extensions are causing problems, so no, I dont think you can do cleanup. I guess the only thing you could do is release a seperate cleanup script for users to run if they wanted.
  • or you could add a custom uninstall feature to your extension which cleans up and then runs the "standard" uninstall code.

    i hope mark has more time for vanilla soon. i think extension developers need a few more tools in the vanilla core. cleanup, a global "extension settings" standard (Attachments and JQThickbox handle this perfectly), etc.
  • Ahh that's a good idea ithcy.
This discussion has been closed.