Skip to content

Commit 1cc14d7

Browse files
committed
wip
1 parent 63809f7 commit 1cc14d7

File tree

3 files changed

+65
-12
lines changed

3 files changed

+65
-12
lines changed

src/Fields/URL.php

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

src/Resources/Resource.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -612,12 +612,12 @@ public function routeMatched(RouteMatched $event): void
612612
{
613613
$event->route->defaults('resource', $this->getKey());
614614

615-
$controller = $event->route->getController();
615+
if ($controller = $event->route->getController()) {
616+
$controller->middleware($this->getRouteMiddleware());
616617

617-
$controller->middleware($this->getRouteMiddleware());
618-
619-
if (! is_null($this->getPolicy())) {
620-
$controller->authorizeResource($this->getModel(), 'resourceModel');
618+
if (! is_null($this->getPolicy())) {
619+
$controller->authorizeResource($this->getModel(), 'resourceModel');
620+
}
621621
}
622622

623623
$this->__routeMatched($event);

src/Root.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Root
2727
*
2828
* @var string
2929
*/
30-
public const string VERSION = '2.7.3';
30+
public const string VERSION = '2.7.4';
3131

3232
/**
3333
* The registered booting callbacks.

0 commit comments

Comments
 (0)