Skip to content

Commit 50ee137

Browse files
agnonymifox
authored andcommitted
+ | Column - Listing - Link
1 parent 6d51bd7 commit 50ee137

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace A17\Twill\Services\Listings\Columns;
4+
5+
use A17\Twill\Models\Contracts\TwillModelContract;
6+
use A17\Twill\Services\Listings\TableColumn;
7+
use Closure;
8+
9+
class Link extends TableColumn
10+
{
11+
protected ?Closure $url = null;
12+
13+
protected Closure|string|null $content = null;
14+
15+
protected bool $targetBlank = false;
16+
17+
public function url(Closure $urlFunction): static
18+
{
19+
$this->url = $urlFunction;
20+
return $this;
21+
}
22+
23+
public function content(Closure|string $contentFunction): static
24+
{
25+
$this->content = $contentFunction;
26+
return $this;
27+
}
28+
29+
public function shouldOpenInNewWindow(bool $shouldOpenInNewWindow = true): static
30+
{
31+
$this->targetBlank = $shouldOpenInNewWindow;
32+
return $this;
33+
}
34+
35+
protected function getRenderValue(TwillModelContract $model): string
36+
{
37+
$url = null;
38+
if (($urlFunction = $this->url) !== null) {
39+
$url = $urlFunction($model);
40+
}
41+
42+
$content = null;
43+
if (($contentFunction = $this->content) !== null) {
44+
if (is_string($this->content)) {
45+
$content = $this->content;
46+
} else {
47+
$content = $contentFunction($model);
48+
}
49+
}
50+
51+
if ($url === null) {
52+
return $content;
53+
}
54+
55+
return
56+
'<a href="' . $url . '"' . ($this->targetBlank ? ' target="_blank"' : '') . '>'
57+
. ($content ?? $url)
58+
. '</a>';
59+
}
60+
}

0 commit comments

Comments
 (0)