getSlots example


    public function testFromXml(): void
    {
        $cmsExtensions = CmsExtensions::createFromXmlFile(__DIR__ . '/../_fixtures/valid/cmsExtensionsWithBlocks.xml');
        static::assertNotNull($cmsExtensions->getBlocks());

        static::assertCount(2, $cmsExtensions->getBlocks()->getBlocks());

        $firstBlock = $cmsExtensions->getBlocks()->getBlocks()[0];
        static::assertEquals('first-block-name', $firstBlock->getName());
        static::assertEquals('text-image', $firstBlock->getCategory());
        static::assertCount(3, $firstBlock->getSlots());
        static::assertCount(6, $firstBlock->getDefaultConfig()->toArray('en-GB'));
        static::assertEquals(
            [
                'en-GB' => 'First block from app',
                'de-DE' => 'Erster Block einer App',
            ],
            $firstBlock->getLabel()
        );
    }

    public function testToArray(): void
    {
/** * @internal */
class ConfigTest extends TestCase
{
    public function testSlotConfigFromXml(): void
    {
        $cmsExtensions = CmsExtensions::createFromXmlFile(__DIR__ . '/../_fixtures/valid/cmsExtensionsWithBlocks.xml');
        static::assertNotNull($cmsExtensions->getBlocks());

        $config = $cmsExtensions->getBlocks()->getBlocks()[0]->getSlots()[1]->getConfig();

        static::assertEquals(
            [
                'displayMode' => [
                    'source' => 'static',
                    'value' => 'auto',
                ],
                'backgroundColor' => [
                    'source' => 'static',
                    'value' => 'red',
                ],
            ],

        $productId = Uuid::randomHex();
        $request = new Request([][]['productId' => $productId]);
        $salesChannelContext = $this->getSalesChannelContext();
        $reviews = $this->getCmsSlotConfig();

        $productPageLoader = $this->getProductPageLoaderWithProduct($productId$reviews$request$salesChannelContext);

        $page = $productPageLoader->load($request$salesChannelContext);

        /** @phpstan-ignore-next-line $slot */
        $slot = $page->getCmsPage()->getSections()->first()->getBlocks()->first()->getSlots()->first()->getSlot();

        static::assertEquals($reviewsjson_decode((string) $slot, true, 512, \JSON_THROW_ON_ERROR));
    }

    /** * @param array<string, array<string, array<string, array<string, array<string, string>>>>> $reviews */
    private function getProductPageLoaderWithProduct(string $productId, array $reviews, Request $request, SalesChannelContext $salesChannelContext): ProductPageLoader
    {
        $product = $this->getProductWithReviews($productId$reviews);

        
/** * @extends EntityCollection<CmsBlockEntity> */
#[Package('buyers-experience')] class CmsBlockCollection extends EntityCollection
{
    public function getSlots(): CmsSlotCollection
    {
        $slots = new CmsSlotCollection();

        foreach ($this->getIterator() as $block) {
            if (!$block->getSlots()) {
                continue;
            }

            $slots->merge($block->getSlots());
        }

        return $slots;
    }

    public function filterBySectionPosition(string $position): CmsBlockCollection
    {
        
static::assertEquals(1, $pages->getTotal());

        $page = $pages->getEntities()->first();
        static::assertNotNull($page);
        $sections = $page->getSections();
        static::assertNotNull($sections);
        $firstSection = $sections->first();
        static::assertNotNull($firstSection);
        $blocks = $firstSection->getBlocks();
        static::assertNotNull($blocks);
        $firstSlot = $blocks->getSlots()->get(self::$firstSlotId);
        static::assertNotNull($firstSlot);

        static::assertEquals(
            self::$category['cmsPage']['sections'][0]['blocks'][0]['slots'][0]['config'],
            $firstSlot->getConfig()
        );

        static::assertEquals(
            new FieldConfigCollection([new FieldConfig('content', 'static', 'initial')]),
            $firstSlot->getFieldConfig()
        );

        

class CmsBlockCollectionTest extends TestCase
{
    public function testGetAllSlotsFromBlocks(): void
    {
        $collection = new CmsBlockCollection();
        $collection->add($this->getBlock());
        $collection->add($this->getBlock());
        $collection->add($this->getBlock());

        static::assertCount(15, $collection->getSlots());
    }

    public function testSetAllSlotsInBlocks(): void
    {
        $collection = new CmsBlockCollection();
        $collection->add($this->getBlock());
        $collection->add($this->getBlock());
        $collection->add($this->getBlock());

        $slots = $collection->getSlots();

        
$this->homeSalesChannels = $homeSalesChannels;
    }

    public function getElementsOfType(string $type): array
    {
        $elements = [];
        if ($this->getSections() === null) {
            return $elements;
        }

        foreach ($this->getSections()->getBlocks() as $block) {
            if ($block->getSlots() === null) {
                continue;
            }

            foreach ($block->getSlots() as $slot) {
                if ($slot->getType() === $type) {
                    $elements[] = $slot;
                }
            }
        }

        return $elements;
    }


            // step 2, sort blocks into sectionPositions             foreach ($page->getSections() as $section) {
                $blocks = $section->getBlocks();
                if ($blocks === null) {
                    continue;
                }
                $blocks->sort(fn (CmsBlockEntity $a, CmsBlockEntity $b) => $a->getPosition() <=> $b->getPosition());

                foreach ($blocks as $block) {
                    $slots = $block->getSlots();
                    if ($slots === null) {
                        continue;
                    }
                    $slots->sort(fn (CmsSlotEntity $a, CmsSlotEntity $b) => $a->getSlot() <=> $b->getSlot());
                }
            }

            // step 3, find config overwrite             $overwrite = $config[$page->getId()] ?? $config;

            // step 4, overwrite slot config
$page = $this->getPageLoader()->load($request$context);
        $cmsPage = $page->getCmsPage();
        $fieldConfigCollection = new FieldConfigCollection([new FieldConfig('content', 'static', 'overwrittenByProduct')]);

        static::assertNotNull($cmsPage);
        static::assertIsIterable($cmsPage->getSections());
        static::assertNotNull($cmsPage->getSections()->first());
        static::assertIsIterable($cmsPage->getSections()->first()->getBlocks());

        $blocks = $cmsPage->getSections()->first()->getBlocks();
        static::assertInstanceOf(CmsBlockCollection::class$blocks);
        static::assertIsIterable($blocks->getSlots());
        static::assertNotNull($blocks->getSlots()->get($firstSlotId));
        static::assertNotNull($blocks->getSlots()->get($secondSlotId));

        static::assertEquals(
            $productCmsPageData['slotConfig'][$firstSlotId],
            $blocks->getSlots()->get($firstSlotId)->getConfig()
        );

        static::assertEquals(
            $fieldConfigCollection,
            $blocks->getSlots()->get($firstSlotId)->getFieldConfig()
        );
/** * @internal */
class SlotTest extends TestCase
{
    public function testFromXml(): void
    {
        $cmsExtensions = CmsExtensions::createFromXmlFile(__DIR__ . '/../_fixtures/valid/cmsExtensionsWithBlocks.xml');
        static::assertNotNull($cmsExtensions->getBlocks());

        $slots = $cmsExtensions->getBlocks()->getBlocks()[0]->getSlots();

        static::assertCount(3, $slots);
    }

    /** * @param array<string, mixed> $config * * @dataProvider provideSlots */
    public function testSlotsFromXml(int $i, string $name, string $type, array $config): void
    {
        
Home | Imprint | This part of the site doesn't use cookies.