1

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;
}
  • First of all your $block->image is usually an object of type ElementCriteriaModel 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
  • @RobinSchambach I found that the syntax above worked well when working with a matrix instance however I will try what you've suggested as it would be good to remove the [0] index. Yes, my assets have public URLs, I have a feeling there's an issue with requesting a specific file type being PDF as the folder is set to restrict uploads to only PDFs – Filth Jan 23 '18 at 17:29
  • I know it works with the other syntax, I just wanted to mention it. What does $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
  • I get the error: Internal Server Error Call to a member function getUrl() on null – Filth Jan 23 '18 at 17:59
  • That means your block does not contain an element. So you need to check if there is a block or if $block->downloadablePdf[0] is null 'downloadablePDF' => $block->downloadablePdf[0]? $block->downloadablePdf[0]->getUlr() : '' – Robin Schambach Jan 23 '18 at 18:01
3

You have to check if your matrix element has a block or not with the code $block->downloadablePdf[0]->url you'll access the first element in your matrix but when there is no it will throw an error.

'downloadablePDF' => $block->downloadablePdf[0]? $block->downloadablePdf[0]->getUlr() : ''

will check if there is an asset and return the url or an empty string if there is no asset

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.