如何从 Shopware 5 中的插件创建文档模板?
How to create a document template from the plugin in Shopware 5?
我正在尝试通过我的插件创建一个文档,该文档在 MyPlugin/Resources/views/documents/index_foo.tpl
中并且我有
通过订阅事件注册模板 Enlight_Controller_Action_PostDispatchSecure
还添加了模板目录$this->template->addTemplateDir($this->pluginDir . '/Resources/views/');
及其在 table s_core_documents
中的条目,但是当我尝试通过转到 Basic Settings > PDF Document Creation > Preview
来预览它时
我收到这个错误
Ups! 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
时,它起作用了。
知道如何使用我自己的插件实现这一点吗?
在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}
在我的 Subscriber/TemplateRegistration.php
方法 onCollectTemplateDirectories()
中调整事件的 return 后,上述错误得到解决
现在调整后是这个样子
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 在社区中解释了这背后的技术原因
you have to use the one or the other depending on the event you are
subscribing to. you need the adjust the return of the event. if you
subscribe to another event, you need the template service
现在终于当我按下模板的预览按钮时 Basic Settings > PDF Document Creation > Preview
我看到了 content/text 模板中写的任何内容并且没有更多错误。
希望这对以后的人有所帮助。
我正在尝试通过我的插件创建一个文档,该文档在 MyPlugin/Resources/views/documents/index_foo.tpl
中并且我有
通过订阅事件注册模板 Enlight_Controller_Action_PostDispatchSecure
还添加了模板目录$this->template->addTemplateDir($this->pluginDir . '/Resources/views/');
及其在 table s_core_documents
中的条目,但是当我尝试通过转到 Basic Settings > PDF Document Creation > Preview
我收到这个错误
Ups! 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
时,它起作用了。
知道如何使用我自己的插件实现这一点吗?
在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}
在我的 Subscriber/TemplateRegistration.php
onCollectTemplateDirectories()
中调整事件的 return 后,上述错误得到解决
现在调整后是这个样子
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 在社区中解释了这背后的技术原因
you have to use the one or the other depending on the event you are subscribing to. you need the adjust the return of the event. if you subscribe to another event, you need the template service
现在终于当我按下模板的预览按钮时 Basic Settings > PDF Document Creation > Preview
我看到了 content/text 模板中写的任何内容并且没有更多错误。
希望这对以后的人有所帮助。