Skip to content

Commit

Permalink
Merge pull request #5315 from Laravel-Backpack/add-crudbutton-positio…
Browse files Browse the repository at this point in the history
…n-method

add crudbutton position method
  • Loading branch information
pxpm authored Sep 19, 2023
2 parents 4cd8e5d + 675dd9d commit 58160d0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/app/Library/CrudPanel/CrudButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/CrudPanel/CrudPanelButtonsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}

0 comments on commit 58160d0

Please sign in to comment.