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

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.     private SalesChannelCmsPageLoaderInterface $inner;
  24.     public function __construct(SalesChannelCmsPageLoaderInterface $inner)
  25.     {
  26.         $this->inner $inner;
  27.     }
  28.     /**
  29.      * @param array<string, mixed>|null $config
  30.      */
  31.     public function load(
  32.         Request $request,
  33.         Criteria $criteria,
  34.         SalesChannelContext $context,
  35.         ?array $config null,
  36.         ?ResolverContext $resolverContext null
  37.     ): EntitySearchResult {
  38.         $pages $this->inner->load(
  39.             $request,
  40.             $this->addBlockRuleAssociations($criteria),
  41.             $context,
  42.             $config,
  43.             $resolverContext
  44.         );
  45.         /** @var CmsPageEntity $page */
  46.         foreach ($pages as $page) {
  47.             $sections $page->getSections();
  48.             if ($sections === null || \count($sections) === 0) {
  49.                 continue;
  50.             }
  51.             foreach ($sections as $section) {
  52.                 $blocks $section->getBlocks();
  53.                 if ($blocks === null || \count($blocks) === 0) {
  54.                     continue;
  55.                 }
  56.                 $filteredBlocks $blocks->filter(function (CmsBlockEntity $entity) use ($context) {
  57.                     /** @var ArrayEntity|null $blockRule */
  58.                     $blockRule $entity->getExtension('swagCmsExtensionsBlockRule');
  59.                     return $this->getBlockVisibility($blockRule$context);
  60.                 });
  61.                 $section->setBlocks($filteredBlocks);
  62.             }
  63.             $filteredSections $sections->filter(static function (CmsSectionEntity $entity) {
  64.                 $blocks $entity->getBlocks();
  65.                 return $blocks !== null && \count($blocks) > 0;
  66.             });
  67.             $page->setSections($filteredSections);
  68.         }
  69.         return $pages;
  70.     }
  71.     protected function addBlockRuleAssociations(Criteria $criteria): Criteria
  72.     {
  73.         return $criteria
  74.             ->addAssociation(self::BLOCK_RULE_VISIBILITY_RULE_ASSOCIATION_PATH);
  75.     }
  76.     private function getBlockVisibility(?ArrayEntity $blockRuleSalesChannelContext $context): bool
  77.     {
  78.         if ($blockRule === null) {
  79.             return true;
  80.         }
  81.         $visibilityRuleId $blockRule->get('visibilityRuleId');
  82.         $visibilityByRule $visibilityRuleId !== null ? \in_array($visibilityRuleId$context->getRuleIds(), true) : true;
  83.         return $this->applyInverted($blockRule$visibilityByRule);
  84.     }
  85.     private function applyInverted(ArrayEntity $blockRulebool $visibilityByRule): bool
  86.     {
  87.         return $visibilityByRule !== $blockRule->get('inverted');
  88.     }
  89. }