custom/plugins/SwagCmsExtensions/src/Service/Content/Cms/SalesChannel/SalesChannelCmsPageLoaderBlockRuleDecorator.php line 47

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\CmsExtensions\Service\Content\Cms\SalesChannel;
  8. use Shopware\Core\Content\Cms\Aggregate\CmsBlock\CmsBlockEntity;
  9. use Shopware\Core\Content\Cms\Aggregate\CmsSection\CmsSectionEntity;
  10. use Shopware\Core\Content\Cms\CmsPageEntity;
  11. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
  12. use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderInterface;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  15. use Shopware\Core\Framework\Struct\ArrayEntity;
  16. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  17. use Swag\CmsExtensions\Extension\Feature\BlockRule\CmsBlockEntityExtension;
  18. use Symfony\Component\HttpFoundation\Request;
  19. class SalesChannelCmsPageLoaderBlockRuleDecorator implements SalesChannelCmsPageLoaderInterface
  20. {
  21.     public const BLOCK_RULE_ASSOCIATION_PATH 'sections.blocks.' CmsBlockEntityExtension::BLOCK_RULE_ASSOCIATION_PROPERTY_NAME;
  22.     public const BLOCK_RULE_VISIBILITY_RULE_ASSOCIATION_PATH self::BLOCK_RULE_ASSOCIATION_PATH '.visibilityRule';
  23.     /**
  24.      * @var SalesChannelCmsPageLoaderInterface
  25.      */
  26.     private $inner;
  27.     public function __construct(SalesChannelCmsPageLoaderInterface $inner)
  28.     {
  29.         $this->inner $inner;
  30.     }
  31.     /**
  32.      * @param array<string, mixed>|null $config
  33.      */
  34.     public function load(
  35.         Request $request,
  36.         Criteria $criteria,
  37.         SalesChannelContext $context,
  38.         ?array $config null,
  39.         ?ResolverContext $resolverContext null
  40.     ): EntitySearchResult {
  41.         $pages $this->inner->load(
  42.             $request,
  43.             $this->addBlockRuleAssociations($criteria),
  44.             $context,
  45.             $config,
  46.             $resolverContext
  47.         );
  48.         /** @var CmsPageEntity $page */
  49.         foreach ($pages as $page) {
  50.             $sections $page->getSections();
  51.             if ($sections === null || \count($sections) === 0) {
  52.                 continue;
  53.             }
  54.             foreach ($sections as $section) {
  55.                 $blocks $section->getBlocks();
  56.                 if ($blocks === null || \count($blocks) === 0) {
  57.                     continue;
  58.                 }
  59.                 $filteredBlocks $blocks->filter(function (CmsBlockEntity $entity) use ($context) {
  60.                     /** @var ArrayEntity|null $blockRule */
  61.                     $blockRule $entity->getExtension('swagCmsExtensionsBlockRule');
  62.                     return $this->getBlockVisibility($blockRule$context);
  63.                 });
  64.                 $section->setBlocks($filteredBlocks);
  65.             }
  66.             $filteredSections $sections->filter(static function (CmsSectionEntity $entity) {
  67.                 $blocks $entity->getBlocks();
  68.                 return $blocks !== null && \count($blocks) > 0;
  69.             });
  70.             $page->setSections($filteredSections);
  71.         }
  72.         return $pages;
  73.     }
  74.     protected function addBlockRuleAssociations(Criteria $criteria): Criteria
  75.     {
  76.         return $criteria
  77.             ->addAssociation(self::BLOCK_RULE_VISIBILITY_RULE_ASSOCIATION_PATH);
  78.     }
  79.     private function getBlockVisibility(?ArrayEntity $blockRuleSalesChannelContext $context): bool
  80.     {
  81.         if ($blockRule === null) {
  82.             return true;
  83.         }
  84.         $visibilityRuleId $blockRule->get('visibilityRuleId');
  85.         $visibilityByRule $visibilityRuleId !== null \in_array($visibilityRuleId$context->getRuleIds(), true) : true;
  86.         return $this->applyInverted($blockRule$visibilityByRule);
  87.     }
  88.     private function applyInverted(ArrayEntity $blockRulebool $visibilityByRule): bool
  89.     {
  90.         return $visibilityByRule !== $blockRule->get('inverted');
  91.     }
  92. }