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 &lt;nolink&gt;, getLinkFieldValue() in BuildFieldTrait still returns a Url object instead of NULL.</p> <p>The URI is stored as <code>route:&lt;nolink&gt;</code>. The method calls Url::fromUri() on it and then checks $url-&gt;access(), which passes for <code>&lt;nolink&gt;</code>. So the method returns ['url' =&gt; $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-&gt;isRouted() &amp;&amp; $url-&gt;getRouteName() === '&lt;nolink&gt;') {<br>&nbsp; return NULL;<br>}</pre>
issue