はじめに
カスタマイズ領域で進める場合、デザインテンプレートは簡単に切り替えられますが、テンプレートにセットする情報は、コントローラーイベントの EccubeEvents::FRONT_PRODUCT_DETAIL_INITIALIZE
などをフックしても、テンプレートにデータを渡すことはできません。
そんな時は、対象のTwigテンプレートのパスでイベントを監視して、任意のデータをテンプレートに設定できます。
このようにすることで、デフォルトのコントローラーはそのままにしてカスタマイズが可能です。
イベントリスナーを用意する
<?php
declare(strict_types=1);
namespace Customize\EventListener;
use Eccube\Event\TemplateEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductDetailTwigEventListener implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
'Product/detail.twig' => 'handle'
];
}
public function handle(TemplateEvent $event): void
{
$product = $event->getParameter('Product');
if ($product === null) {
return;
}
// do somithing
$event->setParameter('test', 'TEST');
}
}
テンプレートで出力する
app/template/your_template_code/Product/detail.twig
{{ test }}