custom/plugins/WbfkCmsHideParts/src/Service/SalesChannelCmsPageLoaderHideParts.php line 74

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Wbfk\CmsHideParts\Service;
  4. use JetBrains\PhpStorm\ArrayShape;
  5. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\EntityResolverContext;
  6. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
  7. use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\AndFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  16. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Wbfk\CmsHideParts\DataAbstractionLayer\HiddenCmsPartsEntity;
  19. use Wbfk\CmsHideParts\DataAbstractionLayer\HiddenEntityPartsEntity;
  20. class SalesChannelCmsPageLoaderHideParts implements SalesChannelCmsPageLoaderInterface
  21. {
  22.     /**
  23.      * @var SalesChannelCmsPageLoaderInterface
  24.      */
  25.     private SalesChannelCmsPageLoaderInterface $inner;
  26.     /**
  27.      * @var EntityRepositoryInterface
  28.      */
  29.     private EntityRepositoryInterface $hiddenEntityPartsRepository;
  30.     /**
  31.      * @var EntityRepositoryInterface
  32.      */
  33.     private EntityRepositoryInterface $hiddenCmsPartsRepository;
  34.     public function __construct(
  35.         SalesChannelCmsPageLoaderInterface $inner,
  36.         EntityRepositoryInterface $hiddenEntityPartsRepository,
  37.         EntityRepositoryInterface $hiddenCmsPartsRepository
  38.     ) {
  39.         $this->inner $inner;
  40.         $this->hiddenEntityPartsRepository $hiddenEntityPartsRepository;
  41.         $this->hiddenCmsPartsRepository $hiddenCmsPartsRepository;
  42.     }
  43.     public function load(Request $requestCriteria $criteriaSalesChannelContext $context, ?array $config null, ?ResolverContext $resolverContext null): EntitySearchResult
  44.     {
  45.         $hiddenParts $this->getHiddenParts($criteria->getIds(), $context$resolverContext);
  46.         if (!empty($hiddenParts['slot'])) {
  47.             $criteria->getAssociation('sections.blocks.slots')->addFilter(
  48.                 new NotFilter(MultiFilter::CONNECTION_AND, [new EqualsAnyFilter('id'array_values($hiddenParts['slot']))])
  49.             );
  50.         }
  51.         if (!empty($hiddenParts['block'])) {
  52.             $criteria->getAssociation('sections.blocks')->addFilter(
  53.                 new NotFilter(MultiFilter::CONNECTION_AND, [new EqualsAnyFilter('id'array_values($hiddenParts['block']))])
  54.             );
  55.         }
  56.         if (!empty($hiddenParts['section'])) {
  57.             $criteria->getAssociation('sections')->addFilter(
  58.                 new NotFilter(MultiFilter::CONNECTION_AND, [new EqualsAnyFilter('id'array_values($hiddenParts['section']))])
  59.             );
  60.         }
  61.         return $this->inner->load($request$criteria$context$config$resolverContext);
  62.     }
  63.     /**
  64.      * @param array $ids
  65.      * @param SalesChannelContext $context
  66.      * @param ResolverContext|null $resolverContext
  67.      * @return array|array[]
  68.      */
  69.     #[ArrayShape(['slot' => "array"'block' => "array"'section' => "array"])]
  70.     private function getHiddenParts(array $idsSalesChannelContext $context, ?ResolverContext $resolverContext null): array
  71.     {
  72.         /*
  73.          * Load all hidden parts of the template/CMS-Page
  74.          * We will sort them into 'slot', 'block' and 'section'
  75.          */
  76.         $hiddenParts = ['slot' => [], 'block' => [], 'section' => []];
  77.         //$criteria = new Criteria($ids);
  78.         $criteria = new Criteria();
  79.         $criteria->addFilter(new EqualsFilter('pageId'$ids[0]));
  80.         $hiddenCmsParts $this->hiddenCmsPartsRepository->search($criteria$context->getContext());
  81.         foreach ($hiddenCmsParts->getElements() as $hiddenCmsParts) {
  82.             /** @var HiddenCmsPartsEntity $hiddenCmsParts */
  83.             $cmsId $hiddenCmsParts->getCmsId();
  84.             $hiddenParts[$hiddenCmsParts->getCmsType()][$cmsId] = $cmsId;
  85.         }
  86.         /*
  87.          * Load all hidden parts of the entity
  88.          * We will also sort them into 'slot', 'block' and 'section'
  89.          * and therefore merge/override the hidden parts set by the template
  90.          */
  91.         if ($resolverContext instanceof EntityResolverContext) {
  92.             $entityName $resolverContext->getDefinition()->getEntityName();
  93.             $entityId $resolverContext->getEntity()->getUniqueIdentifier();
  94.             $criteria = new Criteria();
  95.             $criteria->addFilter(
  96.                 new AndFilter([
  97.                     new EqualsFilter('entity'$entityName),
  98.                     new EqualsFilter('entityId'$entityId)
  99.                 ])
  100.             );
  101.             $hiddenEntityParts $this->hiddenEntityPartsRepository->search($criteria$context->getContext());
  102.             foreach ($hiddenEntityParts->getElements() as $hiddenEntityParts) {
  103.                 /** @var HiddenEntityPartsEntity $hiddenEntityParts */
  104.                 $cmsId $hiddenEntityParts->getCmsId();
  105.                 $cmsType $hiddenEntityParts->getCmsType();
  106.                 if ($hiddenEntityParts->isHidden()) {
  107.                     $hiddenParts[$cmsType][$cmsId] = $cmsId;
  108.                 } else {
  109.                     // Unset, if it was set by the template
  110.                     unset($hiddenParts[$cmsType][$cmsId]);
  111.                 }
  112.             }
  113.         }
  114.         return $hiddenParts;
  115.     }
  116. }