custom/plugins/fourtwosixPopupsGenerator/src/Subscriber/PageIndex.php line 33

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace fourtwosix\PopupsGenerator\Subscriber;
  3. use Shopware\Core\System\SystemConfig\SystemConfigService;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
  7. use Shopware\Core\Framework\Struct\ArrayEntity;
  8. class PageIndex implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var SystemConfigService
  12.      */
  13.     private $systemConfigService;
  14.     private $container;
  15.     public function __construct(SystemConfigService $systemConfigService$container)
  16.     {
  17.         $this->systemConfigService $systemConfigService;
  18.         $this->container $container;
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return[
  23.             FooterPageletLoadedEvent::class => 'onPageIndex'
  24.         ];
  25.     }
  26.     public function onPageIndex(FooterPageletLoadedEvent $event)
  27.     {
  28.         $settings $this->systemConfigService->getDomain('fourtwosixPopupsGenerator.settings');
  29.         $mediaRepository $this->container->get('media.repository');
  30.         
  31.         $currentMedia "";
  32.         if(isset($settings['fourtwosixPopupsGenerator.settings.popupImage'])){
  33.             $mediaId $settings['fourtwosixPopupsGenerator.settings.popupImage'];
  34.             
  35.             $criteria = new Criteria([$mediaId]);
  36.             $criteria->addAssociation('mediaFolder');
  37.             $currentMedia $mediaRepository
  38.                 ->search($criteria$event->getContext())
  39.                 ->get($mediaId);
  40.             if($currentMedia == null){
  41.                 $currentMedia "";
  42.             }else{
  43.                 $currentMedia $currentMedia->getUrl();
  44.             }
  45.         }
  46.         
  47.         $config = array(
  48.             "id" =>  '0p1o2p3u4p5',
  49.             "title" => isset($settings['fourtwosixPopupsGenerator.settings.popupTitle']) ? $settings['fourtwosixPopupsGenerator.settings.popupTitle'] : "",
  50.             "description" => isset($settings['fourtwosixPopupsGenerator.settings.popupDescription']) ? $settings['fourtwosixPopupsGenerator.settings.popupDescription'] : "",
  51.             "link" => isset($settings['fourtwosixPopupsGenerator.settings.popupLink']) ? $settings['fourtwosixPopupsGenerator.settings.popupLink'] : "",
  52.             "button_text" => isset($settings['fourtwosixPopupsGenerator.settings.popupButton']) ? $settings['fourtwosixPopupsGenerator.settings.popupButton'] : "",
  53.             "display" => isset($settings['fourtwosixPopupsGenerator.settings.popupActive']) ? $settings['fourtwosixPopupsGenerator.settings.popupActive'] : false,
  54.             "position" => isset($settings['fourtwosixPopupsGenerator.settings.popupPosition']) ? $settings['fourtwosixPopupsGenerator.settings.popupPosition'] : "center",
  55.             "background" => $currentMedia
  56.         );
  57.         $extensions = new ArrayEntity;
  58.         $extensions['popupConfig'] = $config;
  59.         $event->getPagelet()->addExtension("popupExtension",$extensions);
  60.     }
  61. }