diff --git a/src/app/Library/CrudPanel/CrudButton.php b/src/app/Library/CrudPanel/CrudButton.php index 4cda26aa91..4ad7182b63 100644 --- a/src/app/Library/CrudPanel/CrudButton.php +++ b/src/app/Library/CrudPanel/CrudButton.php @@ -162,6 +162,32 @@ public function view($value) return $this->save(); } + /** + * Set the button position. Defines where the button will be shown + * in regard to other buttons in the same stack. + * + * @param string $stack 'beginning' or 'end' + * @return CrudButton + */ + public function position($position) + { + switch ($position) { + case 'beginning': + $this->makeFirst(); + break; + + case 'end': + $this->makeLast(); + break; + + default: + abort(500, "Unknown button position - please use 'beginning' or 'end'."); + break; + } + + return $this; + } + /** * Sets the meta that will be available in the view. * diff --git a/tests/Unit/CrudPanel/CrudPanelButtonsTest.php b/tests/Unit/CrudPanel/CrudPanelButtonsTest.php index af616eba0e..5ffaf413e2 100644 --- a/tests/Unit/CrudPanel/CrudPanelButtonsTest.php +++ b/tests/Unit/CrudPanel/CrudPanelButtonsTest.php @@ -285,4 +285,25 @@ private function addTestButton($buttonName) { CrudButton::name(array_values($this->{$buttonName})); } + + public function testMovingTheButtonUsingPosition() + { + $button1 = CrudButton::name('lineTest')->to('line')->view('crud::buttons.test')->type('view'); + $button2 = CrudButton::name('lineTest2')->to('line')->view('crud::buttons.test')->type('view')->position('beginning'); + $this->assertEquals($button2->toArray(), $this->crudPanel->buttons()->first()->toArray()); + $button2->position('end'); + $this->assertEquals($button1->toArray(), $this->crudPanel->buttons()->first()->toArray()); + } + + public function testThrowsErrorInUnknownPosition() + { + try { + $button1 = CrudButton::name('lineTest')->to('line')->view('crud::buttons.test')->type('view')->position('unknown'); + } catch (\Throwable $e) { + } + $this->assertEquals( + new \Symfony\Component\HttpKernel\Exception\HttpException(500, 'Unknown button position - please use \'beginning\' or \'end\'.'), + $e + ); + } }