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

ApplicationTrait::onInit() #12439

Merged
merged 3 commits into from
Dec 9, 2022
Merged

ApplicationTrait::onInit() #12439

merged 3 commits into from
Dec 9, 2022

Conversation

brandonkelly
Copy link
Member

@brandonkelly brandonkelly commented Dec 9, 2022

Description

Adds a new onInit() method to craft\base\ApplicationTrait, which can be used to simplify initialization code for plugins and modules.

It accepts a callback function that contains code that shouldn’t be executed until the app (including all installed plugins) is fully initialized, such as rendering a Twig template, executing an element query, etc.

If Craft is already initialized, the callback function will be called immediately. Otherwise it will be called on EVENT_INIT.

class Plugin extends \craft\base\Plugin
{
    public function init(): void
    {
        // Defer most setup tasks until Craft is fully initialized
        Craft::$app->onInit(function() {
            // ...
        });
    }

    // ...
}

Related issues

@brandonkelly brandonkelly requested a review from a team as a code owner December 9, 2022 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant