applab Posted October 24, 2021 Share Posted October 24, 2021 Hi All, I'm making my first serious foray into module development and I've discovered some behaviour which I found surprising... If I uninstall my module from the backend but do not delete the file, then I refresh a front-end page that is using that module, I would expect to see some kind of error but what happens is the module gets automatically re-installed. I can mitigate this behaviour by wrapping my front-end code in: if(modules()->isInstalled('MyModule')) { ... } but that doesn't seem like the best approach if the module is used in multiple places. Is there something I can add to my modules install() method to prevent it getting auto-installed in this situation? Thanks. Link to comment Share on other sites More sharing options...
Zeka Posted October 24, 2021 Share Posted October 24, 2021 Hi @applab Probably somewhre in your code you are calling $modules->get("MyModule"). So just check before getting this module. if ($modules->isInstalled("MyModule")) { echo $modules->get("MyModule")->renderBlock(); } Also if this module is required by another module it will be installed (Not sure, but looks like it behaves in this way). Link to comment Share on other sites More sharing options...
applab Posted October 24, 2021 Author Share Posted October 24, 2021 Hi @Zeka, Yes, I am using $modules->get("MyModule") in my front-end template. I was just surprised to see MyModule get installed as a result of that code rather than throwing an error, which is what I was expecting. I've achieved my original goal of preventing the install in that situation but I've realised that I still need to use isInstalled() in my front-end templates to avoid runtime errors so, thanks. Link to comment Share on other sites More sharing options...
kongondo Posted October 25, 2021 Share Posted October 25, 2021 5 hours ago, applab said: I was just surprised to see MyModule get installed as a result of that code rather than throwing an error, which is what I was expecting. That is the default behaviour. Quote If the module is not installed, but is installable, it will be installed, instantiated, and initialized. If you don't want that behavior, call $modules->isInstalled('ModuleName') as a conditional first. Alternatively, you can use the method $modules->getModule() with noInstall option. <?php namespace ProcessWire; $modules->getModule('MyModule', ['noInstall' => true]); Quote noInstall (bool): Specify true to prevent a non-installed module from installing from this request. (default=false) 2 Link to comment Share on other sites More sharing options...
applab Posted October 25, 2021 Author Share Posted October 25, 2021 Thanks @kongondo, that is precisely the info I needed. Next time I'll RTFM a bit more before I post! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now