Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify precautions around hooks which dispatch on install/upgrade #1306

Merged
merged 1 commit into from
Apr 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions docs/apis/core/hooks/index.md
Original file line number Diff line number Diff line change
@@ -296,9 +296,11 @@ Any guidelines for callback priority should be described in hook descriptions if

:::caution

Callbacks _are executed during system installation and all upgrades_, the callback
methods must verify the plugin is in correct state. Often the easiest way is to
use function during_initial_install() or version string from the plugin configuration.
Hooks may be dispatched at any time, _including during system installation and upgrade_ (for example `before_http_headers`). Callback methods for such hooks must take extra care to ensure the plugin is properly initialised and that the database is available if database calls are made (as the database does not exist during site installation).

The `during_initial_install()` function can be used to check whether the the site is currently being installed, and `get_config('your_pluginname', 'version')` are two ways to conditionally make database queries or use API functions. `isset($CFG->upgraderunning)` can also be used to test if an upgrade is running. Failing to implement these checks may render the web install/upgrade page unusable.

Please note that the legacy component callback system did _not_ call the `lib.php` callbacks during installation or upgrade. As such, when porting these callbacks to hooks, you may need to implement additional checks as described above.

:::