Skip to content

Commit 747fff9

Browse files
Improved the Link widget's URL generation logic
1 parent c5e5889 commit 747fff9

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

Changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# AppShell Changelog
22

3+
## Unreleased
4+
##### 2026-XX-YY
5+
6+
- Changed (Improved) the link widget so that it doesn't generate the link if it's denied by `onlyIf` or `onlyIfCan` directives
7+
38
## 4.17.0
49
##### 2026-04-20
510

src/Widgets/Link.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ public static function create(Theme $theme, array $options = []): Link
6868
public function render($data = null): string
6969
{
7070
$url = $this->url;
71+
$can = $this->can($data);
7172
return $this->renderViewFromTheme('link', array_merge($this->options, [
72-
'can' => $this->can($data),
73+
'can' => $can,
7374
'text' => $this->text->render($data),
74-
'url' => $url($data, $this),
75+
'url' => $can ? $url($data, $this) : null,
7576
]));
7677
}
7778

tests/Unit/LinkWidgetTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,22 @@ public function class_can_be_added_to_the_inner_text_wrapper()
188188
$html = $link->render();
189189
$this->assertStringContainsString('<span class="text-muted">', $html);
190190
}
191+
192+
/** @test */
193+
public function it_does_not_generate_a_route_if_it_is_not_allowed()
194+
{
195+
$link = Link::create(new AppShellTheme(), [
196+
'text' => 'View Plan',
197+
'url' => [
198+
'route' => 'app.this_route_does_not_exist.show',
199+
'parameters' => ['$model.plan_id'],
200+
],
201+
'onlyIf' => '$model.plan_id',
202+
]);
203+
204+
$model = new \stdClass();
205+
$model->plan_id = null;
206+
207+
$this->assertStringNotContainsString('href="', $link->render($model));
208+
}
191209
}

0 commit comments

Comments
 (0)