Skip to content

Commit 36401ce

Browse files
committed
Allow both Alpine method and regular JS call in cell actions
1 parent 68d8161 commit 36401ce

4 files changed

Lines changed: 61 additions & 18 deletions

File tree

Component/Grid/GridViewModel.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -472,19 +472,10 @@ protected function getAdditionalActions(DataObject $item): array
472472
$actionsFromBlock = (array)$this->getBlock()->getRowActions();
473473
if (!empty($actionsFromBlock)) {
474474
foreach ($actionsFromBlock as $action) {
475-
$params = ['id' => $item->getId()];
476-
477-
$url = null;
478-
if (isset($action['url'])) {
479-
$url = $this->urlFactory->create()->getUrl($action['url'], $params);
480-
}
481-
482-
$jsMethod = null;
483-
if (isset($action['jsMethod'])) {
484-
$jsMethod = $action['jsMethod'];
485-
}
486-
487-
$actions[] = $this->cellActionFactory->create($action['label'], $url, $jsMethod);
475+
$actions = $this->cellActionFactory->createFromData([
476+
'id' => $item->getId(),
477+
...$action,
478+
]);
488479
}
489480
}
490481

Grid/Cell/CellAction.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public function __construct(
88
private string $label,
99
private ?string $url = null,
1010
private ?string $jsMethod = null,
11+
private ?string $alpineMethod = null,
1112
) {
1213
}
1314

@@ -35,4 +36,14 @@ public function getJsMethod(): ?string
3536
{
3637
return (string)$this->jsMethod;
3738
}
39+
40+
public function hasAlpineMethod(): bool
41+
{
42+
return !empty($this->alpineMethod);
43+
}
44+
45+
public function getAlpineMethod(): ?string
46+
{
47+
return (string)$this->alpineMethod;
48+
}
3849
}

Grid/Cell/CellActionFactory.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,52 @@
33
namespace Loki\AdminComponents\Grid\Cell;
44

55
use Magento\Framework\ObjectManagerInterface;
6+
use Magento\Framework\UrlFactory;
67

78
class CellActionFactory
89
{
910
public function __construct(
1011
private ObjectManagerInterface $objectManager,
12+
private UrlFactory $urlFactory,
1113
) {
1214
}
1315

14-
public function create(string $label, ?string $url = null, ?string $jsMethod = null): CellAction
16+
public function createFromData(array $data): CellAction
1517
{
18+
$params = [];
19+
if (isset($data['id'])) {
20+
$params['id'] = $data['id'];
21+
}
22+
23+
$url = null;
24+
if (isset($data['url'])) {
25+
$url = $this->urlFactory->create()->getUrl($data['url'], $params);
26+
}
27+
28+
$jsMethod = null;
29+
if (isset($data['jsMethod'])) {
30+
$jsMethod = $data['jsMethod'];
31+
}
32+
33+
$alpineMethod = null;
34+
if (isset($data['alpineMethod'])) {
35+
$alpineMethod = $data['alpineMethod'];
36+
}
37+
38+
return $this->create($data['label'], $url, $jsMethod, $alpineMethod);
39+
}
40+
41+
public function create(
42+
string $label,
43+
?string $url = null,
44+
?string $jsMethod = null,
45+
?string $alpineMethod = null,
46+
): CellAction {
1647
return $this->objectManager->create(CellAction::class, [
17-
'label' => $label,
18-
'url' => $url,
19-
'jsMethod' => $jsMethod,
48+
'label' => $label,
49+
'url' => $url,
50+
'jsMethod' => $jsMethod,
51+
'alpineMethod' => $alpineMethod,
2052
]);
2153
}
2254
}

view/adminhtml/templates/grid/table.phtml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,16 @@ $state = $viewModel->getState();
138138
) ?></a>
139139
<?php endif; ?>
140140
<?php if ($cellAction->hasJsMethod()): ?>
141-
<a @click="<?= /* @noEscape */ $cellAction->getJsMethod() ?>"><?= $escaper->escapeHtml(
141+
<a
142+
data-id="<?= $item->getId() ?>"
143+
onClick="<?= /* @noEscape */ $cellAction->getJsMethod() ?>"><?= $escaper->escapeHtml(
144+
$cellAction->getLabel()
145+
) ?></a>
146+
<?php endif; ?>
147+
<?php if ($cellAction->hasAlpineMethod()): ?>
148+
<a
149+
data-id="<?= $item->getId() ?>"
150+
@click="<?= /* @noEscape */ $cellAction->getAlpineMethod() ?>"><?= $escaper->escapeHtml(
142151
$cellAction->getLabel()
143152
) ?></a>
144153
<?php endif; ?>

0 commit comments

Comments
 (0)