Skip to content

[Content]: Revise "Extending Twig" > "Registering the Extension" #790

Description

@alinnert

Product

Craft CMS

Description

The docs mention this code in order to register Twig extensions:

if (Craft::$app->getRequest()->getIsSiteRequest()) {
    // Instantiate + register the extension:
    $extension = new mynamespace\myplugin\twig\Extension();
    Craft::$app->getView()->registerTwigExtension($extension);
}

And then states:

The getIsSiteRequest() check is optional. If your extension provides features that will be useful in system emails (commonly triggered from the control panel), or will be used when rendering templates from console requests (less common, but still valid), you may want to register it in all contexts.

However, since Craft 4.2.0 custom Twig functions are also used in CKEditor if it renders deeply nested partial templates that include CKEditor themselves. This results in issues like craftcms/ckeditor#248 and craftcms/cms#19639 (and I've also been asking for more context on this matter in craftcms/ckeditor#627). That's why I'd consider the getIsSiteRequest() check as actually harmful in Twig extension modules because now there are quite a few cases where Twig extensions are required in the CP as well.

Either this should get mentioned in the docs or the check should get removed completely (assuming the behavior of CKEditor doesn't change).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions