custom/plugins/NetzpPowerPack6/src/Subscriber/FrontendSubscriber.php line 210

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace NetzpPowerPack6\Subscriber;
  3. use Shopware\Core\Content\Category\CategoryEntity;
  4. use Shopware\Core\Content\Category\CategoryEvents;
  5. use Shopware\Core\Content\Cms\Exception\PageNotFoundException;
  6. use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderInterface;
  7. use Shopware\Core\Content\Product\ProductEntity;
  8. use Shopware\Core\Content\Product\ProductEvents;
  9. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Shopware\Storefront\Event\ThemeCompilerConcatenatedStylesEvent;
  14. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  15. use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
  16. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\DependencyInjection\ContainerInterface;
  19. class FrontendSubscriber implements EventSubscriberInterface
  20. {
  21.     private $container;
  22.     private $cmsPageLoader;
  23.     private $config;
  24.     public function __construct(ContainerInterface $container,
  25.                                 SalesChannelCmsPageLoaderInterface $cmsPageLoader,
  26.                                 SystemConfigService $config)
  27.     {
  28.         $this->container $container;
  29.         $this->cmsPageLoader $cmsPageLoader;
  30.         $this->config $config;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             HeaderPageletLoadedEvent::class             => 'loadCmsHeader',
  36.             FooterPageletLoadedEvent::class             => 'loadCmsFooter',
  37.             CheckoutFinishPageLoadedEvent::class        => 'loadFinishPage',
  38.             ProductEvents::PRODUCT_LOADED_EVENT         => 'onProductLoaded',
  39.             CategoryEvents::CATEGORY_LOADED_EVENT       => 'onCategoryLoaded',
  40.             ThemeCompilerConcatenatedStylesEvent::class => 'onThemeCompiler'
  41.         ];
  42.     }
  43.     public function onThemeCompiler(ThemeCompilerConcatenatedStylesEvent $event)
  44.     {
  45.         $config $this->config->get('NetzpPowerPack6.config'$event->getSalesChannelId());
  46.         if (array_key_exists('excludefontawesome'$config) && $config['excludefontawesome']) {
  47.             return;
  48.         }
  49.         $bundle $this->container->get('kernel')->getBundles()['NetzpPowerPack6'] ?? null;
  50.         if($bundle === null) {
  51.             return;
  52.         }
  53.         $projectDir $bundle->getPath() ?? null;
  54.         if($projectDir === null) {
  55.             return;
  56.         }
  57.         $styles $event->getConcatenatedStyles();
  58.         $includeFile $projectDir "/Resources/app/storefront/src/csslibs/fontawesome/fontawesome";
  59.         $styles .= "@import '" $includeFile "';" PHP_EOL;
  60.         $event->setConcatenatedStyles($styles);
  61.     }
  62.     public function onCategoryLoaded(EntityLoadedEvent $event)
  63.     {
  64.         if($event->getContext()->getScope() != 'user') {
  65.             return;
  66.         }
  67.         if ( ! property_exists($event->getContext()->getSource(), 'salesChannelId')) {
  68.             return;
  69.         }
  70.         $config $this->config->get('NetzpPowerPack6.config'$event->getContext()
  71.                                ->getSource()
  72.                                ->getSalesChannelId());
  73.         if ( ! $config['snippetsproduct']) {
  74.             return;
  75.         }
  76.         $twig = clone $this->container->get('twig');
  77.         foreach($event->getEntities() as $entity) {
  78.             if(get_class($entity) !== CategoryEntity::class) {
  79.                 continue;
  80.             }
  81.             $translated $entity->getTranslated();
  82.             $tplDescription $twig->createTemplate($translated['description'] ?? '');
  83.             $description $tplDescription->render();
  84.             $translated['description'] = $description;
  85.             $entity->setDescription($description);
  86.             // Name führt hier zu Problemen, u.a. <title> wird nicht ersetzt
  87.             /*
  88.             $tplName = $twig->createTemplate($translated['name'] ?? '');
  89.             $translated['name'] = $tplName->render();
  90.             $breadcrumbs = $translated['breadcrumb'];
  91.             foreach($breadcrumbs as &$bc) {
  92.                 $tplBreadcrumb = $twig->createTemplate($bc ?? '');
  93.                 $bc = $tplBreadcrumb->render();
  94.             }
  95.             $translated['breadcrumb'] = $breadcrumbs;
  96.             */
  97.             $entity->assign(['translated' => $translated]);
  98.         }
  99.     }
  100.     public function onProductLoaded(EntityLoadedEvent $event)
  101.     {
  102.         if($event->getContext()->getScope() != 'user') {
  103.             return;
  104.         }
  105.         if (property_exists($event->getContext()->getSource(), 'salesChannelId')) {
  106.             $config $this->config->get('NetzpPowerPack6.config'$event->getContext()->getSource()->getSalesChannelId());
  107.         }
  108.         else {
  109.             $config $this->config->get('NetzpPowerPack6.config');
  110.         }
  111.         if( ! $config['snippetsproduct']) {
  112.             return;
  113.         }
  114.         $twig = clone $this->container->get('twig');
  115.         foreach($event->getEntities() as $entity) {
  116.             if(get_class($entity) !== SalesChannelProductEntity::class &&
  117.                 get_class($entity) !== ProductEntity::class) {
  118.                 continue;
  119.             }
  120.             $translated $entity->getTranslated();
  121.             $tplName $twig->createTemplate($translated['name'] ?? '');
  122.             $translated['name'] = $tplName->render();
  123.             $tplDescription $twig->createTemplate($translated['description'] ?? '');
  124.             $translated['description'] = $tplDescription->render();
  125.             $entity->assign(['translated' => $translated]);
  126.         }
  127.     }
  128.     public function loadCmsheader(HeaderPageletLoadedEvent $event): void
  129.     {
  130.         $request $event->getRequest();
  131.         $context $event->getSalesChannelContext();
  132.         $customFields $context->getSalesChannel()->getTranslated()['customFields'];
  133.         if($customFields && array_key_exists('netzp_powerpack6_header_cms_show'$customFields) &&
  134.                             array_key_exists('netzp_powerpack6_header_cms_id'$customFields) &&
  135.                             $customFields['netzp_powerpack6_header_cms_id'] != null) {
  136.             $id strtolower(trim($customFields['netzp_powerpack6_header_cms_id']));
  137.             if ($id != '') {
  138.                 $criteria = new Criteria([$id]);
  139.                 $pages $this->cmsPageLoader->load($request$criteria$context);
  140.                 if (!$pages->has($id)) {
  141.                     throw new PageNotFoundException($id);
  142.                 }
  143.             }
  144.             $event->getPagelet()->assign([
  145.                 'netzp_header_cms_page'     => $pages->get($id),
  146.                 'netzp_header_cms_position' => $customFields['netzp_powerpack6_header_cms_show'] ?? 0,
  147.                 'netzp_header_cms_sticky'   => $customFields['netzp_powerpack6_header_cms_sticky'] ?? false
  148.             ]);
  149.         }
  150.     }
  151.     public function loadCmsFooter(FooterPageletLoadedEvent $event): void
  152.     {
  153.         $request $event->getRequest();
  154.         $context $event->getSalesChannelContext();
  155.         $customFields $context->getSalesChannel()->getTranslated()['customFields'];
  156.         if($customFields && array_key_exists('netzp_powerpack6_footer_cms_show'$customFields)
  157.                          && array_key_exists('netzp_powerpack6_footer_cms_id'$customFields)
  158.                          && $customFields['netzp_powerpack6_footer_cms_id'] != null) {
  159.             $id strtolower(trim($customFields['netzp_powerpack6_footer_cms_id']));
  160.             $position $customFields['netzp_powerpack6_footer_cms_show'];
  161.             if ($id != '') {
  162.                 $criteria = new Criteria([$id]);
  163.                 $pages $this->cmsPageLoader->load($request$criteria$context);
  164.                 if (!$pages->has($id)) {
  165.                     throw new PageNotFoundException($id);
  166.                 }
  167.             }
  168.             $event->getPagelet()->assign([
  169.                 'netzp_footer_cms_page'     => $pages->get($id),
  170.                 'netzp_footer_cms_position' => $position
  171.             ]);
  172.         }
  173.     }
  174.     public function loadFinishPage(CheckoutFinishPageLoadedEvent $event): void
  175.     {
  176.         $request $event->getRequest();
  177.         $context $event->getSalesChannelContext();
  178.         $customFields $context->getSalesChannel()->getTranslated()['customFields'];
  179.         if($customFields && array_key_exists('netzp_powerpack6_finish_cms_show'$customFields)
  180.             && array_key_exists('netzp_powerpack6_finish_cms_id'$customFields)
  181.             && $customFields['netzp_powerpack6_finish_cms_id'] != null) {
  182.             $id strtolower(trim($customFields['netzp_powerpack6_finish_cms_id']));
  183.             $position $customFields['netzp_powerpack6_finish_cms_show'];
  184.             if ($id != '') {
  185.                 $criteria = new Criteria([$id]);
  186.                 $pages $this->cmsPageLoader->load($request$criteria$context);
  187.                 if (!$pages->has($id)) {
  188.                     throw new PageNotFoundException($id);
  189.                 }
  190.             }
  191.             $event->getPage()->assign([
  192.                 'netzp_finish_cms_page'     => $pages->get($id),
  193.                 'netzp_finish_cms_position' => $position
  194.             ]);
  195.         }
  196.     }
  197. }