Render title of internal page link

I have a simple Link field referring to page links only:

fields:
  link:
    type: link
    options:
      - page

When I render the link in my template like this:

<a href="<?= $page->link()->toUrl() ?>"><?= $page->link() ?></a>

I don’t get the title of the linked page rendered, but only the UUID, like this:

<a href="mysite.test/linkto/page"> page://RgUzW7i5EdYOpNYx </a>

is it possible to render the title of the linked page without adding a new “link title” field?

Thank you.

In your case, since you only have page links, convert the field value to a page object:

$page->link()->toPage()?->title();

Thanks - This solved my issue.

Can I ask what the ? does after the ()

The ? is a so-called null-safe operator. It prevents a “Calling a member method xx on null error” if what you expect to return an object returns null. In this example, if the link field is empty or the page reference stored in that field does not exist, $page->link()->toPage() would return null instead of a page object, an calling the title() method would subsequently throw an error.

It’s a shorter form of writing

if ($p = $page->link()->toPage()) {
  echo $p->title();
}

Using this shorthand is not always useful, though, as it might result in empty tags.