Returning correct URL with URL

Hello everyone,

When I’m trying to retrieve link URL from a file from another subpage with this call

<?php 
$code = $data->codecall();
foreach($page->site()->index()->filterBy('receipe_code', '*=', $code) as $item): ?>
  <a href="<?= $item->receipe_file()->link() ?>">Herunterladen</a></li>
<?php endforeach ?>

I’m getting url to file in root - that’s not working because the file is another subpage.
But when I’m using this:

 <li><?= $item->receipe_file()->link() ?></li>

I’m gettin correct link but there I cann’t place predefined Text for download like abowe. How can I fix this?

What is. stored in the recipe_file field?

File name with extension. This is how it looks in blueprint:

receipe_file:
label: Rezeptdatei
type: select
icon: file
options: query
query:
fetch: files
value: ‘{{filename}}’
text: ‘{{filename}}’
flip: true

and subpage txt

----
Odin-file: 135-018-d.docx
----

I have never used the link field method with a file. While this is probably fool proof, I would check if the file actually exists and only output a link if this is. the. case.

if($file = $item->receipe_file()->toFile()) {
  echo $file->url();
}

(Just on a side note because the spelling correction always kicks in, the word receipedoesn’t exist in English. Either recipe or receipt, depending on meaning)

1 Like

Thanks, that worked!