<?php
/*
* This file is part of Refine
*
* Copyright(c) 2023 Refine Co.,Ltd. All Rights Reserved.
*
* https://www.re-fine.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\RefineProductImageAlt42;
use Eccube\Event\TemplateEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class Event implements EventSubscriberInterface
{
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
'Product/detail.twig' => 'productDetailTemplate',
'@admin/Product/product.twig' => 'onTemplateAdminProductEdit',
];
}
public function productDetailTemplate(TemplateEvent $event)
{
$Product = $event->getParameter('Product');
if (count($Product->getProductImage()) > 0) {
$source = $event->getSource();
if (preg_match('/alt=\"{{ loop.first \? Product.name : \'\' }}"/', $source, $result)) {
$search = $result[0];
$replace = 'alt="{{ ProductImage.alt_text ? ProductImage.alt_text : Product.name }}"';
$source = str_replace($search, $replace, $source);
}
$event->setSource($source);
$source = $event->getSource();
if (preg_match('/<div class=\"slideThumb\" data-index=\"{{ loop.index0 }}\"><img src=\"{{ asset\(ProductImage\, \'save_image\'\) }}" alt="/',$source, $result)){
$search = $result[0];
$replace = $search.'{{ ProductImage.alt_text ? ProductImage.alt_text : Product.name }}';
$source = str_replace($search, $replace, $source);
}
$event->setSource($source);
}
}
public function onTemplateAdminProductEdit(TemplateEvent $event)
{
$source = $event->getSource();
if (preg_match('/<div id\="upload\-zone".+>\n.+\n.+\n.+\n.+\n.+\n.+\n.+\n.+\n.+\n.+\n.+/', $source, $result)) {
$search = $result[0];
$text = "{{ include('@RefineProductImageAlt42/admin/Product/product_images_alt.twig') }}";
$replace = $search.$text;
$source = str_replace($search, $replace, $source);
}
$event->setSource($source);
}
}