Skip to content

Commit d824251

Browse files
committed
Allow button to be defined both via XML layout and custom PHP
1 parent f5f74af commit d824251

6 files changed

Lines changed: 59 additions & 26 deletions

File tree

Component/Grid/GridViewModel.php

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Loki\AdminComponents\Grid\Filter\StaticFilterInterface;
99
use Loki\AdminComponents\Grid\MassAction\MassActionFactory;
1010
use Loki\AdminComponents\Grid\State\FilterState;
11+
use Loki\AdminComponents\Ui\ButtonInterface;
1112
use Magento\Framework\Data\OptionSourceInterface;
1213
use Magento\Framework\DataObject;
1314
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
@@ -270,31 +271,39 @@ public function getCellTemplates(): array
270271
}
271272

272273
/**
273-
* @return Button[]
274+
* @return ButtonInterface[]
274275
*/
275276
public function getButtons(): array
276277
{
277-
$buttons = [];
278278
$buttonActions = (array)$this->getBlock()->getButtonActions();
279-
if (!empty($buttonActions)) {
280-
foreach ($buttonActions as $buttonAction) {
281-
if (!is_array($buttonAction)) {
282-
continue;
283-
}
279+
if (empty($buttonActions)) {
280+
return [];
281+
}
284282

285-
if (!isset($buttonAction['method']) || !isset($buttonAction['label'])) {
286-
continue;
287-
}
283+
$buttons = [];
288284

289-
$buttons[] = $this->buttonFactory->create(
290-
(string)$buttonAction['method'],
291-
(string)$buttonAction['label'],
292-
isset($buttonAction['cssClass']) ? (string)$buttonAction['cssClass'] : '',
293-
isset($buttonAction['url']) ? (string)$buttonAction['url'] : '',
294-
isset($buttonAction['subButtons']) ? (string)$buttonAction['subButtons'] : [],
295-
isset($buttonAction['primary']) ? (bool)$buttonAction['primary'] : false,
296-
);
285+
foreach ($buttonActions as $buttonAction) {
286+
if ($buttonAction instanceof ButtonInterface) {
287+
$buttons[] = $buttonAction;
288+
continue;
297289
}
290+
291+
if (!is_array($buttonAction)) {
292+
continue;
293+
}
294+
295+
if (!isset($buttonAction['method']) || !isset($buttonAction['label'])) {
296+
continue;
297+
}
298+
299+
$buttons[] = $this->buttonFactory->create(
300+
(string)$buttonAction['method'],
301+
(string)$buttonAction['label'],
302+
isset($buttonAction['cssClass']) ? (string)$buttonAction['cssClass'] : '',
303+
isset($buttonAction['url']) ? (string)$buttonAction['url'] : '',
304+
isset($buttonAction['subButtons']) ? (string)$buttonAction['subButtons'] : [],
305+
isset($buttonAction['primary']) ? (bool)$buttonAction['primary'] : false,
306+
);
298307
}
299308

300309
return $buttons;
@@ -317,6 +326,7 @@ public function getMassActions(): array
317326
$massAction['label'],
318327
$massAction['url'],
319328
isset($massAction['url_parameters']) ? $massAction['url_parameters'] : [],
329+
$massAction,
320330
);
321331
}
322332
}

Grid/MassAction/MassAction.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public function __construct(
1111
protected string $label,
1212
protected string $url,
1313
protected array $urlParameters = [],
14+
protected array $data = [],
1415
) {
1516
}
1617

Grid/MassAction/MassActionFactory.php

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

14-
15-
public function create(string $label, string $url, array $urlParameters = []): MassActionInterface
16-
{
14+
public function create(
15+
string $label,
16+
string $url,
17+
array $urlParameters = [],
18+
array $data = []
19+
): MassActionInterface {
1720
return $this->objectManager->create(MassAction::class, [
1821
'label' => $label,
1922
'url' => $url,
2023
'urlParameters' => $urlParameters,
24+
'data' => $data,
2125
]);
2226
}
2327
}

Ui/Button.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Loki\AdminComponents\Ui;
44

5-
class Button
5+
class Button implements ButtonInterface
66
{
77
public function __construct(
88
private string $method,

Ui/ButtonInterface.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Loki\AdminComponents\Ui;
4+
5+
interface ButtonInterface
6+
{
7+
public function getMethod(): string;
8+
9+
public function getLabel(): string;
10+
11+
public function getCssClass(): string;
12+
13+
/**
14+
* @return Button[]
15+
*/
16+
public function getSubButtons(): array;
17+
18+
public function getUrl(): string;
19+
}

view/adminhtml/templates/ui/button.phtml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
<?php
22
declare(strict_types=1);
33

4-
/** @version 0.3.3 */
5-
4+
use Loki\AdminComponents\Ui\ButtonInterface;
65
use Magento\Framework\Escaper;
76
use Magento\Framework\View\Element\Template;
8-
use Loki\AdminComponents\Ui\Button;
97

8+
/** @version 0.3.3 */
109
/** @var Escaper $escaper */
1110
/** @var Template $block */
12-
/** @var Button $button */
11+
/** @var ButtonInterface $button */
1312
$button = $block->getButton();
1413
?>
1514
<?php if ($button->getSubButtons()): ?>

0 commit comments

Comments
 (0)