custom/static-plugins/script-manager/src/Subscriber/scriptManagerSubscriber.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Wexo\ScriptManager\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  7. use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
  8. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Wexo\ScriptManager\Core\Content\ScriptManager\ScriptManagerEntity;
  11. class scriptManagerSubscriber implements EventSubscriberInterface
  12. {
  13.     /** @var EntityRepositoryInterface */
  14.     private $scriptManagerRepository;
  15.     public function __construct(
  16.         EntityRepositoryInterface $scriptManagerRepository
  17.     ) {
  18.         $this->scriptManagerRepository $scriptManagerRepository;
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             HeaderPageletLoadedEvent::class => "scriptManagerHeader",
  24.             FooterPageletLoadedEvent::class => "scriptManagerFooter"
  25.         ];
  26.     }
  27.     public function scriptManagerHeader(HeaderPageletLoadedEvent $event): void
  28.     {
  29.         $siteSalesChannelId $this->getSalesChannelId($event);
  30.         $scriptList $this->getCriteria('Header'$event);
  31.         $event->getPagelet()->scripts $this->createScriptList($scriptList$siteSalesChannelId);
  32.     }
  33.     public function scriptManagerFooter(FooterPageletLoadedEvent $event): void
  34.     {
  35.         $siteSalesChannelId $this->getSalesChannelId($event);
  36.         $scriptList $this->getCriteria('Footer'$event);
  37.         $event->getPagelet()->scripts $this->createScriptList($scriptList$siteSalesChannelId);
  38.     }
  39.     /**
  40.      * @param $location
  41.      * @param $event
  42.      * @return \Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult
  43.      */
  44.     public function getCriteria($location$event)
  45.     {
  46.         $criteria = new Criteria();
  47.         $criteria->addFilter(new EqualsFilter('active'true));
  48.         $criteria->addFilter(new EqualsFilter('location'$location));
  49.         $criteria->addSorting(new FieldSorting('priority'FieldSorting::DESCENDING));
  50.         return $this->scriptManagerRepository->search($criteria$event->getContext());
  51.     }
  52.     /** Function to get sales channel id from $event
  53.      * @param $event
  54.      * @return
  55.      */
  56.     private function getSalesChannelId($event)
  57.     {
  58.         return $event->getSalesChannelContext()->getSalesChannel()->getId();
  59.     }
  60.     /** Function to remove all scripts that is not matched with the current sales channel, as they should not be shown
  61.      * @param $scriptList
  62.      * @param $siteSalesChannelId
  63.      * @return array $scripts
  64.      */
  65.     private function createScriptList($scriptList$siteSalesChannelId)
  66.     {
  67.         $scripts = [];
  68.         /** @var ScriptManagerEntity $scriptEntity */
  69.         foreach ($scriptList->getElements() as $scriptEntity) {
  70.             if (in_array($siteSalesChannelId$scriptEntity->getSalesChannel())) {
  71.                 $scriptEntity->setScript(html_entity_decode($scriptEntity->getScript()));
  72.                 array_push($scripts$scriptEntity);
  73.             };
  74.         }
  75.         return $scripts;
  76.     }
  77. }