I have a PDF folder within assets and having trouble accessing the file path once chosen by the user.
To get something like an image from assets within a matrix I do something like this which works:
'image' => $block->image[0]->url,
I have tried similar code however it seems url is not available?
'downloadablePDF' => $block->downloadablePdf[0]
I need to return the URL so that I can pass the URL to a React Component to make the pdf downloadable.
Here is my full code to fetch data from a matrix block:
function articles($array) {
$articles = [];
foreach ($array as $block) {
switch ($block->type->handle) {
case 'article':
$articles[] = [
'heading' => (string) $block->heading,
'subheading' => $block->subheading,
'text' => (string) $block->text,
'backgroundImage' => (isset($block->backgroundImage)) ? $block->backgroundImage[0]->url : '',
'downloadablePDF' => $block->downloadablePdf[0],
'videoUrl' => (isset($block->videoUrl)) ? $block->videoUrl : '',
'linkUrl' => (isset($block->linkUrl)) ? $block->linkUrl : '',
'size' => (isset($block->size)) ? $block->size->value : '',
'style' => (isset($block->style)) ? $block->style->value : '',
'verticalTextAlignment' => (isset($block->verticalTextAlignment)) ? $block->verticalTextAlignment->value : '',
];
break;
}
}
return $articles;
}
$block->image
is usually an object of typeElementCriteriaModel
so it's better to call the$block->image->first()
method to get the asset. Secondly do your assets have public URLs? You can change the settings from the assets in your cp asset source if they have no public URLs (checkbox) you won't receive a value – Robin Schambach Jan 23 '18 at 17:25$block->downloadablePdf[0]->getUrl()
output? an empty string or does it throw an error when you print it? Not sure if you'll receive json data with'downloadablePDF' => $block->downloadablePdf[0],
because models in Craft 2 have no serialise interface as far as I know – Robin Schambach Jan 23 '18 at 17:57$block->downloadablePdf[0]
is null'downloadablePDF' => $block->downloadablePdf[0]? $block->downloadablePdf[0]->getUlr() : ''
– Robin Schambach Jan 23 '18 at 18:01