Skip to content

Commit 0aa2c58

Browse files
committed
Unify element IDs based on blocks
1 parent d438ff0 commit 0aa2c58

4 files changed

Lines changed: 35 additions & 27 deletions

File tree

Component/ComponentViewModel.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33

44
namespace Loki\Components\Component;
55

6+
use Loki\Components\Util\Block\GetElementId;
7+
use Magento\Framework\App\ObjectManager;
68
use Magento\Framework\View\Element\AbstractBlock;
79
use Loki\Components\Filter\Filter;
810
use Loki\Components\Messages\LocalMessage;
911
use Loki\Components\Validator\Validator;
12+
use Magento\Setup\Model\ObjectManagerProvider;
1013

1114
class ComponentViewModel implements ComponentViewModelInterface
1215
{
@@ -44,8 +47,7 @@ public function getComponentName(): string
4447

4548
public function getElementId(): string
4649
{
47-
$nameInLayout = strtolower((string)$this->block->getNameInLayout());
48-
return preg_replace('/([^a-zA-Z0-9\-]+)/', '-', $nameInLayout);
50+
return ObjectManager::getInstance()->get(GetElementId::class)->execute($this->block);
4951
}
5052

5153
public function getValue(): mixed

Observer/AddDomIdToBlock.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33

44
namespace Loki\Components\Observer;
55

6+
use Loki\Components\Util\Block\GetElementId;
67
use Magento\Framework\Event\Observer;
78
use Magento\Framework\Event\ObserverInterface;
8-
use Magento\Framework\View\Element\AbstractBlock;
99
use Magento\Framework\View\Element\Template;
1010

1111
class AddDomIdToBlock implements ObserverInterface
1212
{
1313
public function __construct(
14+
private readonly GetElementId $getElementId,
1415
private readonly array $blockNames = [],
1516
) {
1617
}
@@ -45,22 +46,11 @@ public function execute(Observer $observer)
4546
return;
4647
}
4748

48-
$elementId = $this->getElementId($block);
49+
$elementId = $this->getElementId->execute($block);
4950
$originalString = '<'.$match[1].$match[2];
5051
$newString = '<'.$match[1].' id="'.$elementId.'"'.$match[2];
5152
$html = preg_replace('#^'.$originalString.'#', $newString, $html);
5253

5354
$transport->setHtml($html);
5455
}
55-
56-
private function getElementId(AbstractBlock $block): string
57-
{
58-
$uniqId = (string)$block->getUniqId();
59-
if (strlen($uniqId) > 0) {
60-
return $uniqId;
61-
}
62-
63-
$nameInLayout = strtolower((string)$block->getNameInLayout());
64-
return preg_replace('#([^a-zA-Z0-9]+)#', '-', $nameInLayout);
65-
}
6656
}

Observer/AddHtmlAttributesToComponentBlock.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Loki\Components\Util\Ajax;
77
use Loki\Components\Util\AppMode;
8+
use Loki\Components\Util\Block\GetElementId;
89
use Magento\Framework\Event\Observer;
910
use Magento\Framework\Event\ObserverInterface;
1011
use Magento\Framework\View\Element\AbstractBlock;
@@ -21,6 +22,7 @@ public function __construct(
2122
private readonly JsDataProvider $jsDataProvider,
2223
private readonly Ajax $ajax,
2324
private readonly AppMode $appMode,
25+
private readonly GetElementId $getElementId
2426
) {
2527
}
2628

@@ -86,7 +88,7 @@ private function getHtmlAttributes(ComponentInterface $component, string $curren
8688
}
8789

8890
$attributes = (array)$block->getData('html_attributes');
89-
$attributes['id'] = $this->getElementId($block);
91+
$attributes['id'] = $this->getElementId->execute($block);
9092
$attributes['x-data'] = $this->jsDataProvider->getComponentName($component);
9193

9294
if ($this->appMode->isDeveloperMode()) {
@@ -119,15 +121,4 @@ private function getJsData(ComponentInterface $component): string
119121

120122
return json_encode($componentData);
121123
}
122-
123-
private function getElementId(AbstractBlock $block): string
124-
{
125-
$uniqId = (string)$block->getUniqId();
126-
if (strlen($uniqId) > 0) {
127-
return $uniqId;
128-
}
129-
130-
$nameInLayout = strtolower((string)$block->getNameInLayout());
131-
return preg_replace('#([^a-zA-Z0-9]+)#', '-', $nameInLayout);
132-
}
133124
}

Util/Block/GetElementId.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Loki\Components\Util\Block;
4+
5+
use Loki\Components\Util\IdConvertor;
6+
use Magento\Framework\View\Element\AbstractBlock;
7+
8+
class GetElementId
9+
{
10+
public function __construct(
11+
private readonly IdConvertor $idConvertor,
12+
) {
13+
}
14+
15+
public function execute(AbstractBlock $block): string
16+
{
17+
$uniqId = (string)$block->getUniqId();
18+
if (strlen($uniqId) > 0) {
19+
return $this->idConvertor->toElementId($uniqId);
20+
}
21+
22+
$nameInLayout = strtolower((string)$block->getNameInLayout());
23+
return $this->idConvertor->toElementId($nameInLayout);
24+
}
25+
}

0 commit comments

Comments
 (0)