<?php
/*
* Plugin Name: JoolenCategoryBannerText42
*
* Copyright(c) joolen inc. All Rights Reserved.
*
* https://www.joolen.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\JoolenCategoryBannerText42;
use Eccube\Common\EccubeConfig;
use Plugin\JoolenCategoryBannerText42\Repository\CategoryBannerRepository;
use Plugin\JoolenCategoryBannerText42\Repository\CategoryBannerTextRepository;
class Event implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
{
private $pluginName = 'JoolenCategoryBannerText42';
protected $eccubeConfig;
protected $categoryBannerRepository;
private $categoryBannerTextRepository;
/**
* Event constructor.
*/
public function __construct(
EccubeConfig $eccubeConfig,
CategoryBannerRepository $categoryBannerRepository,
CategoryBannerTextRepository $categoryBannerTextRepository
) {
$this->eccubeConfig = $eccubeConfig;
$this->categoryBannerRepository = $categoryBannerRepository;
$this->categoryBannerTextRepository = $categoryBannerTextRepository;
}
/**
* フックポイント一覧
*
* @return array
*/
public static function getSubscribedEvents()
{
return [
// フロント画面
// ・banner_text.twigへ画像と文章のデータを差し込む
'JoolenCategoryBannerText42/Resource/template/default/banner_text.twig' => 'addImageAtProductList',
];
}
// ・商品一覧画面に画像と文章を差し込む
public function addImageAtProductList(\Eccube\Event\TemplateEvent $event)
{
log_info('JoolenCategoryBannerText42 trigger addImageAtProductList start');
$parameters = $event->getParameters();
$CategoryBanners = []; // [初期化] バナー
$CategoryBannerText = null; // [初期化] テキストなど
$Category = isset($parameters['Category']) ? $parameters['Category'] : null;
if ($Category) {
// バナー
$CategoryBanners = $this->categoryBannerRepository->findBy(['category_id' => $Category->getId()], ['sort_no' => 'ASC']);
// テキストなど
$CategoryBannerText = $this->categoryBannerTextRepository->findOneBy(['category_id' => $Category->getId()]);
}
// ・レコードがない場合でも差し込む(レコードが無い場合もプラグインテンプレートで変更したtwigを表示したいため)
// パラメータをセット(snippetは手動で追記する方式)
// snippet: {{ include('JoolenCategoryBannerText42/Resource/template/default/banner_text.twig', ignore_missing = true) }}
// snippet追記先: 商品一覧ページ(app/template/default/Product/list.twig)
$parameters['CategoryBanners'] = $CategoryBanners;
$parameters['CategoryBannerText'] = $CategoryBannerText;
$event->setParameters($parameters);
log_info('JoolenCategoryBannerText42 trigger addImageAtProductList finish');
}
}