Skip to content

Commit 68d8161

Browse files
committed
Allow for cell actions to be Alpine methods as well
1 parent 7fbf5fb commit 68d8161

4 files changed

Lines changed: 76 additions & 10 deletions

File tree

Component/Grid/GridViewModel.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,30 @@ public function getMassActions(): array
336336
return $massActions;
337337
}
338338

339+
/**
340+
* @deprecated Use getEditAction() instead
341+
*/
339342
public function getRowAction(DataObject $item): CellAction
343+
{
344+
return $this->getEditAction($item);
345+
}
346+
347+
public function getEditAction(DataObject $item): CellAction
340348
{
341349
// @todo: Retrieve ID from primary key
342350
$editUrl = $this->getEditUrl(['id' => $item->getId()]);
343351

344-
return $this->cellActionFactory->create($editUrl, 'Edit');
352+
return $this->cellActionFactory->create($this->getEditLabel(), $editUrl);
353+
}
354+
355+
private function getEditLabel(): string
356+
{
357+
$editLabel = $this->getBlock()->getEditLabel();
358+
if (!empty($editLabel)) {
359+
return $editLabel;
360+
}
361+
362+
return 'Edit';
345363
}
346364

347365
/**
@@ -403,19 +421,33 @@ private function getGridFilterStateValues(): array
403421
return $values;
404422
}
405423

424+
/**
425+
* @param DataObject $item
426+
*
427+
* @return CellAction[]
428+
*/
406429
public function getCellActions(DataObject $item): array
407430
{
408431
if (false === $this->allowActions()) {
409432
return [];
410433
}
411434

412435
$cellActions = [];
413-
$cellActions[] = $this->getRowAction($item);
436+
437+
if (false === $this->skipEditAction()) {
438+
$cellActions[] = $this->getEditAction($item);
439+
}
440+
414441
$cellActions = array_merge($cellActions, $this->getAdditionalActions($item));
415442

416443
return $cellActions;
417444
}
418445

446+
private function skipEditAction(): bool
447+
{
448+
return (bool) $this->getBlock()->getSkipEditAction();
449+
}
450+
419451
public function allowActions(): bool
420452
{
421453
if ((bool)$this->getBlock()->getData('hide_actions') === true) {
@@ -441,8 +473,18 @@ protected function getAdditionalActions(DataObject $item): array
441473
if (!empty($actionsFromBlock)) {
442474
foreach ($actionsFromBlock as $action) {
443475
$params = ['id' => $item->getId()];
444-
$url = $this->urlFactory->create()->getUrl($action['url'], $params);
445-
$actions[] = $this->cellActionFactory->create($url, $action['label']);
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);
446488
}
447489
}
448490

Grid/Cell/CellAction.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,34 @@
55
class CellAction
66
{
77
public function __construct(
8-
private string $url,
98
private string $label,
9+
private ?string $url = null,
10+
private ?string $jsMethod = null,
1011
) {
1112
}
1213

14+
public function getLabel(): string
15+
{
16+
return $this->label;
17+
}
18+
19+
public function hasUrl(): bool
20+
{
21+
return !empty($this->url);
22+
}
23+
1324
public function getUrl(): string
1425
{
15-
return $this->url;
26+
return (string)$this->url;
1627
}
1728

18-
public function getLabel(): string
29+
public function hasJsMethod(): bool
1930
{
20-
return $this->label;
31+
return !empty($this->jsMethod);
32+
}
33+
34+
public function getJsMethod(): ?string
35+
{
36+
return (string)$this->jsMethod;
2137
}
2238
}

Grid/Cell/CellActionFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ public function __construct(
1111
) {
1212
}
1313

14-
public function create(string $url, string $label): CellAction
14+
public function create(string $label, ?string $url = null, ?string $jsMethod = null): CellAction
1515
{
1616
return $this->objectManager->create(CellAction::class, [
17-
'url' => $url,
1817
'label' => $label,
18+
'url' => $url,
19+
'jsMethod' => $jsMethod,
1920
]);
2021
}
2122
}

view/adminhtml/templates/grid/table.phtml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,16 @@ $state = $viewModel->getState();
132132
<ul>
133133
<?php foreach ($cellActions as $cellAction): ?>
134134
<li>
135+
<?php if ($cellAction->hasUrl()): ?>
135136
<a href="<?= /* @noEscape */ $cellAction->getUrl() ?>"><?= $escaper->escapeHtml(
136137
$cellAction->getLabel()
137138
) ?></a>
139+
<?php endif; ?>
140+
<?php if ($cellAction->hasJsMethod()): ?>
141+
<a @click="<?= /* @noEscape */ $cellAction->getJsMethod() ?>"><?= $escaper->escapeHtml(
142+
$cellAction->getLabel()
143+
) ?></a>
144+
<?php endif; ?>
138145
</li>
139146
<?php endforeach; ?>
140147
</ul>

0 commit comments

Comments
 (0)