Skip to content

[5.x]: craft\fieldlayoutelements\Template rendered in TEMPLATE_MODE_SITE #15926

@commercial-hippie

Description

@commercial-hippie

What happened?

Description

When using craft\fieldlayoutelements\Template in the admin panel ID like to use {% import "_includes/forms" as forms %}, but because it's rendered in SITE mode the CP templates are not loaded.

It works when I change vendor/craftcms/cms/src/fieldlayoutelements/Template.php:103 to TEMPLATE_MODE_CP, but I guess that could cause other issues.

Maybe there is a better way to accomplish this? My end-goal is to use the elementSelectField helper.

Alternatively template mode could be added as a config option:

new Template([
    'template' => '_myplugin/test.twig',
    'templateMode' => View::TEMPLATE_MODE_CP,
]),

Steps to reproduce

Event::on(
    FieldLayout::class,
    FieldLayout::EVENT_CREATE_FORM,
    function (CreateFieldLayoutFormEvent $e) {
        if ($e->element instanceof Course) {
            $e->tabs[] = new FieldLayoutTab([
                'name' => Craft::t('app', 'Test'),
                'layout' => $e->sender,
                'elements' => [
                    new Template([
                        'template' => '_myplugin/test.twig',
                    ]),
                ],
            ]);
        }
    }
);

// test.twig
{% import "_includes/forms" as forms %}

Expected behavior

Should load the helpers.

Actual behavior

Unable to find the template “_includes/forms”

Craft CMS version

5.4.71

PHP version

8.2

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions