1

I have a plugin, which is using another plugin. The issue is that, I am calling some other plugin information in the init function, and I do receive sometimes this error:

2018-11-07 11:44:57 [-][-][k91hcavi6st542afnhsvhdp61u][error][yii\base\ErrorException:8] yii\base\ErrorException: Trying to get property 'mobileDetectService' of non-object in /home/forge/plugins/releases/20181106223004/src/services/CacheService.php:193
Stack trace:
#0 /home/forge/new.trendsideas.com/releases/20181106223038/vendor/craftcms/cms/src/web/ErrorHandler.php(76): yii\base\ErrorHandler->handleError(8, 'Trying to get p...', '/home/forge/plu...', 193)
#1 /home/forge/plugins/releases/20181106223004/src/services/CacheService.php(193): craft\web\ErrorHandler->handleError(8, 'Trying to get p...', '/home/forge/plu...', 193, Array)
#2 /home/forge/plugins/releases/20181106223004/src/services/CacheService.php(126): trendspublishing\craftplugin\services\CacheService->_generateUri('/hubs/bathroom')
#3 /home/forge/plugins/releases/20181106223004/src/Plugin.php(90): trendspublishing\craftplugin\services\CacheService->uriToFilePath('/hubs/bathroom')
#4 /home/forge/new.trendsideas.com/releases/20181106223038/vendor/yiisoft/yii2/base/BaseObject.php(109): trendspublishing\craftplugin\Plugin->init()
#5 /home/forge/new.trendsideas.com/releases/20181106223038/vendor/yiisoft/yii2/base/Module.php(158): yii\base\BaseObject->__construct(Array)
#6 /home/forge/new.trendsideas.com/releases/20181106223038/vendor/craftcms/cms/src/base/Plugin.php(113): yii\base\Module->__construct('craftplugin', Object(craft\web\Application), Array)
#7 [internal function]: craft\base\Plugin->__construct('craftplugin', Object(craft\web\Application), Array)

This is the error in question:

$mobile_detect = MobileDetect::$plugin->mobileDetectService;

I am not able to replicate it, sometimes a simple database reload is helping sometimes not. So that's why I am only guessing, because I do invoke this call from the plugin init function, that's why the other plugin is not ready yet... Any thought, best practice on this one?

4

What I do is I listen for the Plugins::EVENT_AFTER_LOAD_PLUGINS event, and then do my code that relies on other plugins being loaded:

    // Handler: Plugins::EVENT_AFTER_LOAD_PLUGINS
    Event::on(
        Plugins::class,
        Plugins::EVENT_AFTER_LOAD_PLUGINS,
        function () {
            // Do stuff only after all other plugins have loaded
        }
    );
| improve this answer | |
  • Yes, we have this event, and it is working like charm, but in this case is not good. I have to make a decision in realtime (routing decision for cache). Is there any way to manually instantiate the plugin and store it into the proper place? I am trying to use something like this... but guess this is not the best method:( $mobile_detect = (new \superbig\mobiledetect\services\MobileDetectService())->getMobileDetect(); – Visky Máté Nov 6 '18 at 23:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.