我试图通过插件创建一个文档,文档位于MyPlugin/Resources/views/documents/index_foo.tpl内部,我通过订阅事件Enlight_Controller_Action_PostDispatchSecure注册了模板。
还添加了模板目录$this->template->addTemplateDir($this->pluginDir . '/Resources/views/');及其在表s_core_documents中的条目,但当我试图通过转到Basic Settings > PDF Document Creation > Preview预览它时
我知道这个错误
Ups!在费勒主义者自行其是!Wir wurden bereitsüber das问题> informiert und arbeiten a einer L sung,bitte versuchen Sie es in > Kürze erneut。
但是,当我将模板放在themes/Bare/documents中时,它可以工作。
你知道我怎么能用我自己的插件实现这一点吗?
发布于 2021-07-07 13:01:21
在Shopware社区询问之后,Shyim说订阅的事件太晚了,所以请使用这里提到的事件
使用该事件后,我们的Subscriber/TemplateRegistration.php如下所示
class TemplateRegistration implements SubscriberInterface
{
    /**
     * @var Enlight_Template_Manager
     */
    private $template;
    private $pluginDir;
    /**
     * TemplateRegistration constructor.
     * @param Enlight_Template_Manager $template
     * @param $pluginDir
     */
    public function __construct(Enlight_Template_Manager $template, $pluginDir)
    {
        $this->template = $template;
        $this->pluginDir = $pluginDir;
    }
    /**
     * @inheritDoc
     */
    public static function getSubscribedEvents()
    {
        return [
            'Theme_Inheritance_Template_Directories_Collected' => 'onCollectTemplateDirectories',
        ];
    }
    /**
     * @param \Enlight_Event_EventArgs $args
     */
    public function onCollectTemplateDirectories(\Enlight_Event_EventArgs $args)
    {
        $this->template->addTemplateDir($this->pluginDir . '/Resources/views/');
    }
}但即使这样做,我仍然面临错误声明
Ups! Ein Fehler ist aufgetreten!
Die nachfolgenden Hinweise sollten Ihnen weiterhelfen.
Unable to load template snippet 'documents/index_foo.tpl' in engine/Library/Smarty/sysplugins/smarty_internal_templatebase.php on line 127
Stack trace:
#0 engine/Shopware/Components/Document.php(273): Smarty_Internal_TemplateBase->fetch('documents/index...', NULL)
#1 engine/Shopware/Controllers/Backend/Document.php(68): Shopware_Components_Document->render()
#2 engine/Library/Enlight/Controller/Action.php(182): Shopware_Controllers_Backend_Document->indexAction()
#3 engine/Library/Enlight/Controller/Dispatcher/Default.php(478): Enlight_Controller_Action->dispatch('indexAction')
#4 engine/Library/Enlight/Controller/Front.php(228): Enlight_Controller_Dispatcher_Default->dispatch(Object(Enlight_Controller_Request_RequestHttp), Object(Enlight_Controller_Response_ResponseHttp))
#5 engine/Shopware/Kernel.php(191): Enlight_Controller_Front->dispatch()
#6 vendor/symfony/http-kernel/HttpCache/SubRequestHandler.php(85): Shopware\Kernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#7 vendor/symfony/http-kernel/HttpCache/HttpCache.php(477): Symfony\Component\HttpKernel\HttpCache\SubRequestHandler::handle(Object(Shopware\Kernel), Object(Symfony\Component\HttpFoundation\Request), 1, true)
#8 engine/Shopware/Components/HttpCache/AppCache.php(261): Symfony\Component\HttpKernel\HttpCache\HttpCache->forward(Object(Symfony\Component\HttpFoundation\Request), true, NULL)
#9 vendor/symfony/http-kernel/HttpCache/HttpCache.php(267): Shopware\Components\HttpCache\AppCache->forward(Object(Symfony\Component\HttpFoundation\Request), true)
#10 engine/Shopware/Components/HttpCache/AppCache.php(102): Symfony\Component\HttpKernel\HttpCache\HttpCache->pass(Object(Symfony\Component\HttpFoundation\Request), true)
#11 shopware.php(122): Shopware\Components\HttpCache\AppCache->handle(Object(Symfony\Component\HttpFoundation\Request))
#12 {main}在调整了方法onCollectTemplateDirectories()中Subscriber/TemplateRegistration.php中事件的返回后,上述错误得到了解决。
现在它看起来像在调整后
class TemplateRegistration implements SubscriberInterface
{
    /**
     * @var Enlight_Template_Manager
     */
    private $template;
    private $pluginDir;
    /**
     * TemplateRegistration constructor.
     * @param Enlight_Template_Manager $template
     * @param $pluginDir
     */
    public function __construct(Enlight_Template_Manager $template, $pluginDir)
    {
        $this->template = $template;
        $this->pluginDir = $pluginDir;
    }
    /**
     * @inheritDoc
     */
    public static function getSubscribedEvents()
    {
        return [
            'Theme_Inheritance_Template_Directories_Collected' => 'onCollectTemplateDirectories',
        ];
    }
    /**
     * @param \Enlight_Event_EventArgs $args
     */
    public function onCollectTemplateDirectories(\Enlight_Event_EventArgs $args)
    {
        // commented the line below
        // $this->template->addTemplateDir($this->pluginDir . '/Resources/views/');
        // and used this one instead
        $dirs = $args->getReturn();
        $dirs[] = $this->pluginDir . '/Resources/views';
        $args->setReturn($dirs);
    }
}Telgi在社区中解释了这背后的技术原因:
--您必须根据订阅的事件使用其中的一个或另一个。您需要调整事件的返回。如果订阅另一个事件,则需要模板服务。
现在,当我按下模板Basic Settings > PDF Document Creation > Preview的预览按钮时,我看到的是内容/文本,不管是在模板中写什么,都没有错误。
希望这对将来的人有帮助。
https://stackoverflow.com/questions/68286559
复制相似问题