<?php
namespace Plugin\ApgSelectBoxForAddingToCart42;
use Eccube\Event\TemplateEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Twig\Environment;
class Event implements EventSubscriberInterface
{
const TEMPLATE_NAMESPACE = '@ApgSelectBoxForAddingToCart42';
/**
* @var Environment
*/
protected $twig;
public function __construct(
\Twig\Environment $twig
)
{
$this->twig = $twig;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'@admin/Product/product_class.twig' => 'onRenderAdminProductProductClass',
];
}
/**
* [Admin]商品規格ページ表示
* @param TemplateEvent $event
* @return void
* @throws \Twig\Error\LoaderError
*/
public function onRenderAdminProductProductClass(TemplateEvent $event)
{
$source = $event->getSource();
// data
$parameters = $event->getParameters();
// copy js
$pattern = '|\$\(\'select\[id\$=_sale_type\]\'\)\.val\(sale_type\);|s';
$addRow = $this->twig->getLoader()->getSourceContext(self::TEMPLATE_NAMESPACE . '/admin/product_class_copy_js.twig')->getCode();
if (preg_match($pattern, $source, $matches, PREG_OFFSET_CAPTURE)) {
$replacement = $matches[0][0] . $addRow;
$source = preg_replace($pattern, $replacement, $source);
}
$event->setSource($source);
$event->setParameters($parameters);
}
}