在 Shopware 社区中询问后,Shyim 说订阅的事件来不及了,而是使用这里提到的事件
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}
在我调整了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
我看到了模板中写的内容/文本,并且没有更多错误。
希望这对将来的某人有所帮助。