Skip to content

Commit 47e8221

Browse files
committed
Offer a DI-type for other modules to change layout handles before using them
1 parent 7d72bfd commit 47e8221

4 files changed

Lines changed: 64 additions & 0 deletions

File tree

Layout/LayoutHandlerComposite.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Loki\Components\Layout;
4+
5+
use Magento\Framework\View\LayoutInterface;
6+
7+
class LayoutHandlerComposite
8+
{
9+
public function __construct(
10+
private readonly LayoutInterface $layout,
11+
private readonly array $layoutHandlers = []
12+
) {
13+
}
14+
15+
public function execute(array $handles): void
16+
{
17+
$originalHandles = $handles;
18+
$handles = $this->getHandles($handles);
19+
foreach ($handles as $handle) {
20+
$this->layout->getUpdate()->addHandle($handle);
21+
}
22+
23+
foreach (array_diff($originalHandles, $handles) as $handle) {
24+
$this->layout->getUpdate()->removeHandle($handle);
25+
}
26+
}
27+
28+
public function getHandles(array $handles): array
29+
{
30+
foreach ($this->layoutHandlers as $layoutHandler) {
31+
if (false === $layoutHandler instanceof LayoutHandlerInterface) {
32+
continue;
33+
}
34+
35+
$handles = $layoutHandler->get($handles);
36+
}
37+
38+
$handles = array_unique($handles);
39+
40+
return $handles;
41+
}
42+
}

Layout/LayoutHandlerInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Loki\Components\Layout;
5+
6+
interface LayoutHandlerInterface
7+
{
8+
/**
9+
* @param $handles string[]
10+
* @return string[]
11+
*/
12+
public function get(array $handles): array;
13+
}

Util/Controller/LayoutLoader.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@
22

33
namespace Loki\Components\Util\Controller;
44

5+
use Loki\Components\Layout\LayoutHandlerComposite;
56
use Magento\Framework\View\LayoutInterface;
67
use Magento\Framework\View\Result\PageFactory as ResultPageFactory;
78

89
class LayoutLoader
910
{
1011
public function __construct(
1112
private readonly ResultPageFactory $resultPageFactory,
13+
private readonly LayoutHandlerComposite $layoutHandlerComposite
1214
) {
1315
}
1416

1517
public function load(array $handles = []): LayoutInterface
1618
{
19+
$handles = $this->layoutHandlerComposite->getHandles($handles);
1720
$resultPage = $this->resultPageFactory->create();
1821

1922
if ($handles !== []) {

Util/Controller/TargetRenderer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Loki\Components\Util\Controller;
44

5+
use Loki\Components\Layout\LayoutHandlerComposite;
56
use Loki\Components\Util\IdConvertor;
67
use Magento\Framework\Event\Manager as EventManager;
78
use Magento\Framework\View\Element\AbstractBlock;
@@ -13,11 +14,16 @@ public function __construct(
1314
private readonly EventManager $eventManager,
1415
private readonly LayoutInterface $layout,
1516
private readonly IdConvertor $idConvertor,
17+
private readonly LayoutHandlerComposite $layoutHandlerComposite,
18+
private readonly LayoutLoader $layoutLoader
1619
) {
1720
}
1821

1922
public function render(LayoutInterface $layout, array $targetNames): array
2023
{
24+
$handles = $this->layoutHandlerComposite->getHandles($layout->getUpdate()->getHandles());
25+
$layout = $this->layoutLoader->load($handles);
26+
2127
return $this->renderBlocks($layout, $this->getTargetBlockNames($targetNames));
2228
}
2329

0 commit comments

Comments
 (0)