Shopware 5.2.20. Cannot extend template via plugin

不问归期 提交于 2020-01-15 11:08:10

问题


I'm trying to extend template frontend/home/index.tpl via own plugin.

Here my root file:

<?php
namespace TdevExtend;
use Shopware\Components\Plugin;
class TdevExtend extends Plugin
{
   public static function getSubscribedEvents(){
    return [
        'Enlight_Controller_Dispatcher_ControllerPath_Frontend_MyPlugin' =>
            'onDetailPostDispatch'
    ];
   }
   public function onDetailPostDispatch(\Enlight_Event_EventArgs $args)
   {
      $this->container->get('template')->addTemplateDir(
        $this->getPath() . '/Resources/views/'
      );
    return __DIR__ . '/Controllers/Frontend/MyPlugin.php';
   }
}
?>

Here are my Controller MyPlugin.php in dirrectory Controllers\Frontend

public function preDispatch()
   {
    /** @var \Shopware\Components\Plugin $plugin */
    $plugin = $this->get('kernel')->getPlugins()['TdevProductTab'];

    $this->get('template')->addTemplateDir($plugin->getPath() . '/Resources/views/');
}

And template in folder Resources\views\frontend\home\

 {extends file="parent:frontend/home/index.tpl"}
 {block name='frontend_index_content'}
    <div class="">Hello world!</div>
    {$smarty.block.parent}
 {/block}

I have read official documentation and researched plugins examples plugins examples After installing/reinstalling plugin crear cache on backend and delete manualy folder in var/cache. But nothing has helped me.


回答1:


You may want to use the Event Enlight_Controller_Action_PostDispatchSecure_Frontend_Index in getSubscribedEvents instead of Enlight_Controller_Dispatcher_ControllerPath_Frontend_MyPlugin.

You are using the Event Enlight_Controller_Dispatcher_ControllerPath_Frontend_MyPlugin. When looking for controller MyPlugin Shopware will create this Event. So you need to write your own Controller when using this event. But I guess what you want is the event mentioned above. Actually you don't need to write a controller.

<?php
namespace TdevExtend;
use Shopware\Components\Plugin;
class TdevExtend extends Plugin
{
   public static function getSubscribedEvents(){
    return [
        'Enlight_Controller_Action_PostDispatchSecure_Frontend_Index' =>
            'onPostDispatch'
    ];
   }
   public function onPostDispatch(\Enlight_Event_EventArgs $args)
   {
      $this->container->get('Template')->addTemplateDir(
        $this->getPath() . '/Resources/views/'
      );
   }
}
?>


来源:https://stackoverflow.com/questions/42819695/shopware-5-2-20-cannot-extend-template-via-plugin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!