Creating an if statement for PDFs

Hi, I have a foreach template where each a tag will route to different links…some of the items will link to a pdf which is filed under the page and some of the items will link to an external link that I have named “Link” in my content document. What would be the “if” statement to say if there is a pdf, use that but if not, use the link? Here is the code I have for using the link…

<div class="text-container">
<a href="<?= $place->link() ?>"><h3><?= $place->title() ?></h3></a>
<br>

Any thoughts would be greatly appreciated!

What type of field is the link field? And what is stored in that field in case of a pdf file?

It always helps if you provide a bit more code context, e.g. the complete loop.

Hi! Yes, here is the complete loop. What I envision is an “if” statement before the a href line that would give the option to link to the pdf file if it is available for that particular item. Any thoughts would be greatly appreciated!

<?php foreach ($page->children()->listed() as $place): ?>
            <div class= "project-item">
            <img src="<?= $place->image()->url() ?>" alt="photo"> 
            <div class="text-container">
            **<a href="<?= $place->link() ?>"><h3><?= $place->title() ?></h3></a>**
            <br>
            <div class="content">
            <div class="pub">
            <p><?= $place->pub() ?></p>
            <p><?= $place->date() ?></p></div>
            <hr>
            <p><?= $place->text() ?></p>
            </div>
            </div>
            </div>
    
        <?php endforeach ?>

Sorry, have to ask again? What type of field is the link field and what does it contain if it is a pdf?

No problem. So I have a portfolio of my work - some of the items I want to link to are external urls (links) and others are pdf files of designs. The pdfs are embedded within the individual portfolio folders. So basically, I’m trying to do an “if” statement that says if there is no pdf within the page folder, it should link to the file, which is in the page’s content folder.

Sorry if this is confusing! I’m new to Kirby and trying to finish a project for a class! Thank you for your help!

What I would do is use two different fields for the the file or a pdf:

Blueprint:

fields:
  typeOfLink:
    label: Choose link type
    type: radio
    options:
      external: External link
      pdf: PDF file
  pdf:
    type: files
    max: 1
    query: page.files.filterBy('extension', 'pdf')
    when:
      typeOfLink: pdf
  link:
   type: url
   when:
    typeOfLink: external

Template:

<?php if ($place->typeOfLink()->value() === 'external' && $place->link()->isNotEmpty()) : ?>
  <!-- code for external link -->
<?php elseif ($place->typeOfLink()->value() === 'pdf' && $file = $place->pdf()->toFile()) : ?>
  <a href="<?= $file->url() ?>"<This is the link to the file</a>
<?php endif; ?>