Skip to content

Commit dc71fe8

Browse files
authored
Merge pull request #2457 from Laravel-Backpack/reorder-buttons
[4.0][Feature] Implement orderButtons
2 parents bbf23c6 + 086e2db commit dc71fe8

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/app/Library/CrudPanel/Traits/Buttons.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,51 @@ trait Buttons
1010
// BUTTONS
1111
// ------------
1212

13-
// TODO: $this->crud->reorderButtons('stack_name', ['one', 'two']);
13+
/**
14+
* Order the CRUD buttons. If certain button names are missing from the given order array
15+
* they will be pushed to the end of the button collection.
16+
*
17+
*
18+
* @param string $stack Stack where the buttons belongs. Options: top, line, bottom.
19+
* @param array $order Ordered name of the buttons. ['update', 'delete', 'show']
20+
*/
21+
public function orderButtons(string $stack, array $order)
22+
{
23+
$newButtons = collect([]);
24+
$otherButtons = collect([]);
25+
26+
// we get the buttons that belong to the specified stack
27+
$stackButtons = $this->buttons()->reject(function ($item) use ($stack, $otherButtons) {
28+
if ($item->stack != $stack) {
29+
// if the button does not belong to this stack we just add it for merging later
30+
$otherButtons->push($item);
31+
32+
return true;
33+
}
34+
35+
return false;
36+
});
37+
38+
// we parse the ordered buttons
39+
collect($order)->each(function ($btnKey) use ($newButtons, $stackButtons) {
40+
if (! $button = $stackButtons->where('name', $btnKey)->first()) {
41+
abort(500, 'Button name [«'.$btnKey.'»] not found.');
42+
}
43+
$newButtons->push($button);
44+
});
45+
46+
// if the ordered buttons are less than the total number of buttons in the stack
47+
// we add the remaining buttons to the end of the ordered ones
48+
if (count($newButtons) < count($stackButtons)) {
49+
foreach ($stackButtons as $button) {
50+
if (! $newButtons->where('name', $button->name)->first()) {
51+
$newButtons->push($button);
52+
}
53+
}
54+
}
55+
56+
$this->setOperationSetting('buttons', $newButtons->merge($otherButtons));
57+
}
1458

1559
/**
1660
* Add a button to the CRUD table view.

tests/Unit/CrudPanel/CrudPanelButtonsTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,17 @@ public function testRemoveAllButtonsFromUnknownStack()
245245
$this->assertEquals(count($this->defaultButtonNames), count($this->crudPanel->buttons()));
246246
}
247247

248+
public function testOrderButtons()
249+
{
250+
$this->crudPanel->addButton('line', 'update', 'view', 'crud::buttons.update', 'end');
251+
$this->crudPanel->addButton('line', 'show', 'view', 'crud::buttons.show', 'end');
252+
$this->crudPanel->addButton('line', 'test', 'view', 'crud::buttons.test', 'end');
253+
254+
$this->crudPanel->orderButtons('line', ['show', 'test']);
255+
256+
$this->assertEquals(['show', 'test', 'update'], $this->crudPanel->buttons()->pluck('name')->toArray());
257+
}
258+
248259
private function getButtonByName($name)
249260
{
250261
return $this->crudPanel->buttons()->first(function ($value) use ($name) {

0 commit comments

Comments
 (0)