lmscloud-io / mdlcode-docs

5 stars 3 forks source link

When generating hook callbacks automatically add "if (!during_initial_install())" #17

Open marinaglancy opened 3 months ago

marinaglancy commented 3 months ago

Big difference between hook callbacks and legacy lib.php callbacks is that hook callbacks are executed during install and upgrade. There is a caution about it in devdocs: https://moodledev.io/docs/4.4/apis/core/hooks#registering-of-hook-callbacks

Many developers (including myself) already hit this problem when hook callbacks throw exceptions during web installation or upgrade.

It would help if MDLCode automatically generated the following code in the hook callbacks:

        if (during_initial_install() || isset($CFG->upgraderunning)) {
            // Do nothing during installation or upgrade.
            return;
        }

Developers can always remove it if they do want the hook callback to be executed during installation/upgrade

marinaglancy commented 2 months ago

maybe even one more check, that the current plugin is actually installed:

        if (during_initial_install() || isset($CFG->upgraderunning) || !get_config('PLUGINNAME', 'version')) {
            // Do nothing during installation or upgrade.
            return;
        }