I am trying to create a document through my plugin, the document is inside MyPlugin/Resources/views/documents/index_foo.tpl
and i have
registered the template by subscribing to event Enlight_Controller_Action_PostDispatchSecure
Also added the template directory $this->template->addTemplateDir($this->pluginDir . '/Resources/views/');
and its entry in the table s_core_documents
but when i try to preview it by going to Basic Settings > PDF Document Creation > Preview
i get this error
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.
But when i place the template inside themes/Bare/documents
it works.
Any idea how can i achieve this with my own plugin?
After asking it in the Shopware community Shyim said that the event subscribed is too late for this, Instead use the event mentioned here
After using the event our Subscriber/TemplateRegistration.php
looked like this
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/');
}
}
But even doing so i still faced the error stating
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}
The error above got resolved after i adjusted the return of the event in the method onCollectTemplateDirectories()
in my Subscriber/TemplateRegistration.php
Now it looks like this after adjusting
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);
}
}
The technical reason behind this was explained by Telgi in the community that
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
And now finally when i press the preview button of the template Basic Settings > PDF Document Creation > Preview
i see the content/text whatever written in the template and no more errors.
Hope this helps someone in the future.