Skip to content

Commit

Permalink
Add method for setting custom Action href, v6.x (#853)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gappa authored Jan 25, 2023
1 parent f5ca492 commit 806fe8a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/Column/Action.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class Action extends Column
/** @var string|callable */
private $title;

/** @var string|callable|null */
private $customHref;

public function __construct(
DataGrid $grid,
string $key,
Expand All @@ -72,7 +75,7 @@ public function render(Row $row): mixed
// No need to worry.
}

$link = $this->createLink(
$link = $this->getCustomHref($row) ?? $this->createLink(
$this->grid,
$this->href,
$this->getItemParams($row, $this->params) + $this->parameters
Expand Down Expand Up @@ -155,6 +158,37 @@ public function getTitle(Row $row): ?string
* @return static
* @throws DataGridException
*/
public function setCustomHref(string|callable $customHref): self
{
$this->checkPropertyStringOrCallable($customHref, 'customHref');

$this->customHref = $customHref;

return $this;
}

/**
* @throws DataGridException
*/
public function getCustomHref(Row $row): ?string
{
if (!isset($this->customHref)) {
return null;
}

/**
* If user callback was used for setting action customHref, it has to return string
*/
return $this->getPropertyStringOrCallableGetString($row, $this->customHref, 'customHref');
}

/**
* Set attribute class
*
* @param string|callable $class
* @return static
* @throws DataGridException
*/
public function setClass(null|string|callable $class): self
{
$this->checkPropertyStringOrCallable($class, 'class');
Expand Down
24 changes: 24 additions & 0 deletions tests/Cases/ColumnActionTest.phpt
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,30 @@ final class ColumnActionTest extends TestCase
);
}

public function testActionCustomHref(): void
{
$action = $this->grid->addAction('action1', 'Do')->setCustomHref('https://www.example.com/');

Assert::same(
'<a href="https://www.example.com/" class="btn btn-xs btn-default btn-secondary">Do</a>',
$this->render($action)
);

$action = $this->grid->addAction('action2', 'Do')->setCustomHref(fn ($rowItem) => 'https://www.example.com/?name=' . $rowItem['name']);

Assert::same(
'<a href="https://www.example.com/?name=John" class="btn btn-xs btn-default btn-secondary">Do</a>',
$this->render($action)
);

$action = $this->grid->addAction('action3', 'Do')->setCustomHref(fn ($rowItem) => '/preview/user/?id=' . $rowItem['id']);

Assert::same(
'<a href="/preview/user/?id=1" class="btn btn-xs btn-default btn-secondary">Do</a>',
$this->render($action)
);
}

public function testActionConfirm(): void
{
$action = $this->grid->addAction('action', 'Do', 'doStuff!')
Expand Down

0 comments on commit 806fe8a

Please sign in to comment.