custom/plugins/ImnxxLandingpageNavi/src/Subscriber/LandingpageNaviSubscriber.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ImnxxLandingpageNavi\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  7. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  8. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  12. use Shopware\Core\Content\Category\CategoryEntity;
  13. class LandingpageNaviSubscriber implements EventSubscriberInterface {
  14.     /**
  15.      * @var EnityRepository
  16.      */
  17.     private $categoryRepository;
  18.     public function __construct(EntityRepository $categoryRepository) {
  19.         $this->categoryRepository $categoryRepository;
  20.     }
  21.     public static function getSubscribedEvents(): array {
  22.         return [
  23.             GenericPageLoadedEvent::class => 'onPageLoaded',
  24.         ];
  25.     }
  26.     public function onPageLoaded(GenericPageLoadedEvent $event) {
  27.         $page $event->getPage();
  28.         $context $event->getContext();
  29.         $header $page->getHeader();
  30.         if ($header != null) {
  31.             $active_category $header
  32.                 ->getNavigation()
  33.                 ->getActive();
  34.             $parent_category $this->getParentCategory(
  35.                 $context,
  36.                 $active_category->getParentId(),
  37.             );
  38.             $parent_customs_fields $parent_category->getCustomFields();
  39.             if (
  40.                 isset($parent_customs_fields['imnxx_landingpage_navi_show']) &&
  41.                 $parent_customs_fields['imnxx_landingpage_navi_show']
  42.             ) {
  43. //                $customs_fields = $header
  44. //                    ->getNavigation()
  45. //                    ->getActive()
  46. //                    ->getCustomFields();
  47.                 $landingpage_navi_elements $this->getCategoriesByParentId(
  48.                     $context,
  49.                     $parent_category->getId(),
  50.                 );
  51.                 if (count($landingpage_navi_elements) > 0) {
  52.                     $event->getPage()->assign($landingpage_navi_elements);
  53.                     $event->getPage()->assign( ['parent_customs_fields' => $parent_customs_fields] );
  54.                 }
  55.             } else if(
  56.                 isset($active_category->getCustomFields()['imnxx_landingpage_navi_show']) &&
  57.                 $active_category->getCustomFields()['imnxx_landingpage_navi_show']
  58.             ) {
  59. //                $customs_fields = $header
  60. //                    ->getNavigation()
  61. //                    ->getActive()
  62. //                    ->getCustomFields();
  63.                 $landingpage_navi_elements $this->getCategoriesByParentId(
  64.                     $context,
  65.                     $active_category->getId(),
  66.                 );
  67.                 if (count($landingpage_navi_elements) > 0) {
  68.                     $event->getPage()->assign($landingpage_navi_elements);
  69.                     $event->getPage()->assign( ['parent_customs_fields' => $active_category->getCustomFields()] );
  70.                 }
  71.             }
  72.         }
  73.     }
  74.     public function getParentCategory(Context $contextstring $id null): object {
  75.         $criteria = new Criteria();
  76.         $criteria->addAssociation('customFields');
  77.         if ($id != null) {
  78.             $criteria->addFilter(new EqualsFilter('id'$id));
  79.         }
  80.         $category $this->categoryRepository->search($criteria$context)->first();
  81.         if ($category->getParentId() != null && ( !isset($category->getCustomFields()['imnxx_landingpage_navi_show']) || $category->getCustomFields()['imnxx_landingpage_navi_show']!= true ) ) {
  82.             $criteria = new Criteria();
  83.             $criteria->addAssociation('customFields');
  84.             // $criteria->addFilter(new EqualsFilter('customFields.imnxx_landingpage_navi_show', true));
  85.             $criteria->addFilter(new EqualsFilter('id'$category->getParentId()));
  86.             $categoryParent $this->categoryRepository->search($criteria$context)->first();
  87.         }
  88.         if(!isset($categoryParent))
  89.             return $category;
  90.         return $categoryParent;
  91.     }
  92.     public function getCategoriesByParentId(Context $contextstring $parentId null): array {
  93.         $criteria = new Criteria();
  94.         $criteria->addAssociation('customFields');
  95.         $criteria->addAssociation('children');
  96.         if ($parentId == null) {
  97.             return [];
  98.         }
  99.         //Landingpages category ID 32d6d2a9e08146369d513a8dcd7636de
  100.         $criteria->addFilter(new EqualsFilter('parentId'$parentId));
  101.         $unsorted_categories $this->categoryRepository->search($criteria$context)->getElements();
  102.         $sorted_categories = [];
  103.         $last_id '';
  104.         for($i=0$i<count($unsorted_categories); $i++) {
  105.             foreach($unsorted_categories as $category) {
  106.                 if($category->afterCategoryId == $last_id) {
  107.                     $last_id $category->getId();
  108.                     $sorted_categories[$category->getId()] = $category;
  109.                     break;
  110.                 }
  111.             }
  112.         }
  113.         $categories['landingpage_navi_elements'] = $sorted_categories;
  114.         return $categories;
  115.     }
  116. }