dersven
November 24, 2018, 4:52pm
1
i created in my blueprint a structure field to output zum PDF.Files
downloads:
label: Downloads
type: structure
style: table
fields:
label: PDF-Datei
type: select
default:
options: files
but i get no infos about the filename, the size or the link to the file in my template.
how can i access to it
<?php
// get all PDF-Files
$downloads = $page->downloads()->toStructure();
foreach($downloads as $download): ?>
<li><a href="<?= $download->url() ?>"><?= $download->filename() ?> <span>(<?= $download->niceSize() ?>)</span></a></li>
<?php endforeach ?>
You need to put value of the select field through file()
. At the moment you just have the filename stored in the structure field. You need to pick up the value, check the file exists, and work on it from there.
Something like this i think, but not tested…
<?php
// get all PDF-Files
$downloads = $page->downloads()->toStructure();
foreach($downloads as $download):
$pdffile = $page->file($download->PDF-Datei());?>
<?php if($pdffile): ?>
<li><a href="<?= $pdffile->url() ?>"><?= $pdffile->filename() ?> <span>(<?= $pdffile->niceSize() ?>)</span></a></li>
<?php endif ?>
<?php endforeach ?>
Looks like there is an error in the blueprint code you posted above - the select field does not have name key, so you will need to change the code i just gave you above accordingly.
texnixe
November 25, 2018, 12:02am
3
The code @jimbobrjames posted above is correct, but you can shorten it a bit:
<?php
// get all PDF-Files
$downloads = $page->downloads()->toStructure();
foreach($downloads as $download):
<?php if($pdfFile = $download->fieldname()->toFile()): ?>
<li><a href="<?= $pdfFile->url() ?>"><?= $pdfFile->filename() ?> <span>(<?= $pdfFile->niceSize() ?>)</span></a></li>
<?php endif ?>
<?php endforeach ?>
With a blueprint like this:
downloads:
label: Downloads
type: structure
style: table
fields:
fieldname:
label: PDF-Datei
type: select
options: files
Apart from the missing fieldname, there’s also an empty default option, I’m surprised your blueprint worked like this.