Link field Kirby 4 question

So I would like to use the new link field in Kirby 4, I found this documentation about how to use it in my .php template.

Now my question is, can I do some kind of if statement to check if it’s external or internal so I can put the target="_blank" in it.

Yes, of course! Simply add the code for the output in the template:

<a href="<?= $page->link()->toUrl() ?>" target="_blank" rel="noopener"><?= $page->link() ?></a>

Edit: Ops, I overlooked that you still want to distinguish between internal and external links. In my example, all links are opened in a new window.

Ah yes. For example with a file upload you can check for the type:

<?php if($image->type() === 'video'): ?>  <?php endif ?>

Can’t figure out to check for example if type = link, or if type = page.

You can check if the field value starts with http…

Ah of course! Do you have an example how to this in the shortest way in a if statement?

Something like

<?php if (Str::startsWith($page->nameOfLInkField()->value(), 'http')) {
  // do something
} else {
 // do something else

You could move this to a custom field method

1 Like