@@ -16,6 +16,11 @@ class URL extends Text
1616 */
1717 protected Closure $ textResolver ;
1818
19+ /**
20+ * The link attributes.
21+ */
22+ protected array $ linkAttributes = [];
23+
1924 /**
2025 * Create a new field instance.
2126 */
@@ -38,18 +43,66 @@ public function text(Closure|string $value): static
3843 return $ this ;
3944 }
4045
46+ /**
47+ * Set the download attribute.
48+ */
49+ public function download (string $ filename = '' ): static
50+ {
51+ $ this ->linkAttributes ['download ' ] = $ filename ;
52+
53+ return $ this ;
54+ }
55+
56+ /**
57+ * Set the target attribute.
58+ */
59+ public function target (string $ target ): static
60+ {
61+ $ this ->linkAttributes ['target ' ] = $ target ;
62+
63+ return $ this ;
64+ }
65+
66+ /**
67+ * Set the rel attribute.
68+ */
69+ public function rel (string $ rel ): mixed
70+ {
71+ $ this ->linkAttributes ['rel ' ] = $ rel ;
72+
73+ return $ this ;
74+ }
75+
4176 /**
4277 * {@inheritdoc}
4378 */
4479 public function resolveFormat (Request $ request , Model $ model ): ?string
4580 {
4681 if (is_null ($ this ->formatResolver )) {
47- $ this ->formatResolver = fn (Request $ request , Model $ model , mixed $ value ): ?string => is_null ($ value ) ? $ value : sprintf (
48- '<a href="%1$s" title="%1$s"%2$s>%3$s</a> ' ,
49- $ value ,
50- $ this ->isExternal ($ value ) ? ' data-turbo="false" target="_blank" ' : '' ,
51- call_user_func_array ($ this ->textResolver , [$ model ])
52- );
82+ $ this ->formatResolver = function (Request $ request , Model $ model , mixed $ value ): ?string {
83+ if (is_null ($ value )) {
84+ return $ value ;
85+ }
86+
87+ $ attributes = array_merge ($ this ->linkAttributes , [
88+ 'href ' => $ value ,
89+ 'title ' => $ value ,
90+ 'data-turbo ' => $ this ->isExternal ($ value ) ? 'false ' : null ,
91+ 'target ' => $ this ->isExternal ($ value ) ? '_blank ' : ($ this ->linkAttributes ['target ' ] ?? null ),
92+ ]);
93+
94+ $ attributes = array_map (
95+ fn (?string $ value , string $ key ): string => sprintf ('%s="%s" ' , $ key , $ value ),
96+ array_values ($ attributes ),
97+ array_keys ($ attributes )
98+ );
99+
100+ return sprintf (
101+ '<a %s>%s</a> ' ,
102+ implode (' ' , $ attributes ),
103+ call_user_func_array ($ this ->textResolver , [$ model ])
104+ );
105+ };
53106 }
54107
55108 return parent ::resolveFormat ($ request , $ model );
0 commit comments