app/Plugin/RefineProductImageAlt42/Event.php line 32

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Refine
  4.  *
  5.  * Copyright(c) 2023 Refine Co.,Ltd. All Rights Reserved.
  6.  *
  7.  * https://www.re-fine.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\RefineProductImageAlt42;
  13. use Eccube\Event\TemplateEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class Event implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * {@inheritdoc}
  19.      */
  20.     public static function getSubscribedEvents()
  21.     {
  22.         return [
  23.             'Product/detail.twig' => 'productDetailTemplate',
  24.             '@admin/Product/product.twig' => 'onTemplateAdminProductEdit',
  25.         ];
  26.     }
  27.     public function productDetailTemplate(TemplateEvent $event)
  28.     {
  29.         $Product $event->getParameter('Product');
  30.         if (count($Product->getProductImage()) > 0) {
  31.             $source $event->getSource();
  32.             if (preg_match('/alt=\"{{ loop.first \? Product.name : \'\' }}"/'$source$result)) {
  33.                 $search $result[0];
  34.                 $replace 'alt="{{ ProductImage.alt_text ? ProductImage.alt_text : Product.name }}"';
  35.                 $source str_replace($search$replace$source);
  36.             }
  37.             $event->setSource($source);
  38.             $source $event->getSource();
  39.             if (preg_match('/<div class=\"slideThumb\" data-index=\"{{ loop.index0 }}\"><img src=\"{{ asset\(ProductImage\, \'save_image\'\) }}" alt="/',$source$result)){
  40.                 $search $result[0];
  41.                 $replace $search.'{{ ProductImage.alt_text ? ProductImage.alt_text : Product.name }}';
  42.                 $source str_replace($search$replace$source);
  43.             }
  44.             $event->setSource($source);
  45.         }
  46.     }
  47.     public function onTemplateAdminProductEdit(TemplateEvent $event)
  48.     {
  49.         $source $event->getSource();
  50.         if (preg_match('/<div id\="upload\-zone".+>\n.+\n.+\n.+\n.+\n.+\n.+\n.+\n.+\n.+\n.+\n.+/'$source$result)) {
  51.             $search $result[0];
  52.             $text "{{ include('@RefineProductImageAlt42/admin/Product/product_images_alt.twig') }}";
  53.             $replace $search.$text;
  54.             $source str_replace($search$replace$source);
  55.         }
  56.         $event->setSource($source);
  57.     }
  58. }