How can I run a function when my plugin is installed? I tried SafeUp in migrations but it doesn't work.
1 Answer
There's an event you can listen to:
use craft\events\PluginEvent;
use craft\services\Plugins;
// Handler: EVENT_AFTER_INSTALL_PLUGIN
Event::on(
Plugins::class,
Plugins::EVENT_AFTER_INSTALL_PLUGIN,
function (PluginEvent $event) {
if ($event->plugin === $this) {
// Do stuff here, we were just installed
}
}
);
If you generate your plugin scaffolding via pluginfactory.io it will generate the above event for you in your base plugin.