How can I output the absolute URL as link text for internal links on link fields? When using a link field without a link text and just with the URL, Drupal 8/9 outputs the relative path as the link text for internal links. How can I change that to either output the absolute URL or (even better) use the title from the entity in the link as the link text?
I am using the &quot;Separate link text and URL&quot; formatter provided by Drupal core.
Inspired by Link field title from entity/route, I created a preprocess function.
/**
 * Prepares variables for link fields.
 */
function mytheme_preprocess_field__node__field_links(&amp;$variables) {
  /* If it's an internal link and there was no link title provided by the editor, 
then use the entity label as the link text.*/
  $i = 0;
  foreach ($variables['element']['#items'] as &amp;$item) {
    if($item-&gt;getUrl()-&gt;isRouted()) {
      $parameters = $item-&gt;getUrl()-&gt;getRouteParameters();
      if (!empty($parameters)) {
        foreach ($parameters as $entity_type =&gt; $id) {
          if ($entity = Drupal::entityTypeManager()
            -&gt;getStorage($entity_type)
            -&gt;load($id)) {
            $variables['items'][$i]['content']['#url_title'] = Link::fromTextAndUrl($entity-&gt;label(), $entity-&gt;toURL())
              -&gt;toString();
            break;
          }
        }
      }
    }
    $i++;
  }
}