getLinkFieldValue() returns a URL object for <nolink>, causing an empty href that links to the current page
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3575737. -->
Reported by: [balagan](https://www.drupal.org/user/1230072)
Related to !48
>>>
<p><strong>Problem/Motivation</strong></p>
<p>When a link field is set to <nolink>, getLinkFieldValue() in BuildFieldTrait still returns a Url object instead of NULL.</p>
<p>The URI is stored as <code>route:<nolink></code>. The method calls Url::fromUri() on it and then checks $url->access(), which passes for <code><nolink></code>. So the method returns ['url' => $url, ...] rather than NULL.</p>
<p>When the returned Url object is rendered in a Twig template (e.g. href="{{ url }}"), its __toString() outputs an empty string, producing href="". Browsers interpret an empty href as the current page URL, so the element becomes an unintended link to the current page instead of having no link at all.</p>
<p><strong>Proposed resolution</strong></p>
<p>In getLinkFieldValue(), after constructing the Url object, check whether the route is <nolink> and return NULL in that case:</nolink></p>
<pre>$url = Url::fromUri($value[0]['uri']);<br>if ($url->isRouted() && $url->getRouteName() === '<nolink>') {<br> return NULL;<br>}</pre>
issue