Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 13aa462

Browse files
committed
bug #7568 Use formattedValue consistently in UrlField on index and detail pages (lacatoire)
This PR was merged into the 4.x branch. Discussion ---------- Use formattedValue consistently in UrlField on index and detail pages The `UrlField` template uses `field.formattedValue` as the anchor text on the index page but `field.value` on the detail page. This asymmetry prevents `->formatValue()` callbacks from affecting the detail rendering, even though `CommonPostConfigurator` already computes the formatted value for both pages. ## Behavior change `UrlConfigurator::configure()` sets `formattedValue` to a "pretty" URL for every `UrlField` (strips `http://`, `https://`, `www.`, and the trailing `/`; additionally truncates to 32 chars on index only). This PR aligns the detail page rendering with the index rendering, minus the truncation. For `https://www.example.com/path/` the anchor text on detail changes from `https://www.example.com/path/` to `example.com/path`. The `href` attribute keeps using `field.value` (the raw URL), so the link target is unchanged. The `isUnsafe` branch is kept as-is: it intentionally renders the raw value as muted text to signal that a dangerous URL was blocked. Closes #6864 Commits ------- b79ea35 Use formattedValue consistently in UrlField on index and detail pages
2 parents 66fe618 + b79ea35 commit 13aa462

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

templates/crud/field/url.html.twig

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
(see https://web.dev/external-anchors-use-rel-noopener/) #}
66
{% if field.customOptions.get('isUnsafe') %}
77
<span class="text-muted">{{ field.value }}</span>
8-
{% elseif ea().crud.currentAction == 'detail' %}
9-
<a target="_blank" rel="noopener" href="{{ field.value }}">{{ field.value }}</a>
108
{% else %}
119
<a target="_blank" rel="noopener" href="{{ field.value }}">{{ field.formattedValue }}</a>
1210
{% endif %}

tests/Functional/Fields/Text/UrlFieldTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ public function testUrlFieldDisplaysAsLinkOnIndex(): void
4141

4242
public function testUrlFieldDisplaysOnDetail(): void
4343
{
44+
$url = 'https://www.detailed.example.org/path';
4445
$entity = $this->createFieldTestEntity([
45-
'urlField' => 'https://detailed.example.org/path',
46+
'urlField' => $url,
4647
]);
4748

4849
$crawler = $this->client->request('GET', $this->generateDetailUrl($entity->getId()));
@@ -56,8 +57,11 @@ public function testUrlFieldDisplaysOnDetail(): void
5657

5758
if (false !== stripos($label, 'UrlField') || false !== stripos($label, 'Url Field') || false !== stripos($label, 'URL')) {
5859
$urlFieldFound = true;
59-
$fieldValue = $groupCrawler->filter('.field-value');
60-
static::assertStringContainsString('detailed.example.org', $fieldValue->text());
60+
$urlLink = $groupCrawler->filter('.field-value a');
61+
static::assertGreaterThan(0, $urlLink->count(), 'UrlField should render as a link on the detail page');
62+
static::assertSame($url, $urlLink->attr('href'));
63+
// the displayed text should be "pretty" (without https://, www., trailing /)
64+
static::assertSame('detailed.example.org/path', trim($urlLink->text()));
6165
break;
6266
}
6367
}

0 commit comments

Comments
 (0)