【问题标题】:How to create a document template from the plugin in Shopware 5?如何从 Shopware 5 中的插件创建文档模板?
【发布时间】:2021-09-18 00:44:19
【问题描述】:

我正在尝试通过我的插件创建一个文档,该文档在 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 来预览它时

我收到这个错误

起来! Ein Fehler ist aufgetreten! Wir wurden bereits über das Problem > informiert und arbeiten an einer Lösung, bitte versuchen Sie es in > Kürze erneut。


但是当我将模板放在themes/Bare/documents 中时,它可以工作。

知道如何使用我自己的插件来实现这一点吗?

【问题讨论】:

    标签: php symfony shopware shopware5


    【解决方案1】:

    Shyim在Shopware社区询问后说订阅的事件来不及了,改用这里提到的事件

    https://developers.shopware.com/developers-guide/event-list/#theme_inheritance_template_directories_collected

    使用事件后,我们的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}
    

    在我的Subscriber/TemplateRegistration.php 中的onCollectTemplateDirectories() 方法中调整事件返回后,上述错误得到解决

    调整后是这个样子

    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 的预览按钮时,我看到了模板中写的内容/文本,并且没有更多错误。

    希望这对将来的人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2021-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      相关资源
      最近更新 更多