File tree Expand file tree Collapse file tree 1 file changed +60
-0
lines changed
src/Services/Listings/Columns Expand file tree Collapse file tree 1 file changed +60
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments