Answers.Body: <p>You have to check if your matrix element has a block or not with the code <code>$block-&gt;downloadablePdf[0]-&gt;url</code> you'll access the first element in your matrix but when there is no it will throw an error.</p>
  
  <pre><code>'downloadablePDF' =&gt; $block-&gt;downloadablePdf[0]? $block-&gt;downloadablePdf[0]-&gt;getUlr() : ''
  </code></pre>
  
  <p>will check if there is an asset and return the url or an empty string if there is no asset</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-23T18:05:54.613Z
Answers.LastActivityDate: 2018-01-23T18:05:54.613Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I have a PDF folder within assets and having trouble accessing the file path once chosen by the user. </p>
  
  <p>To get something like an image from assets within a matrix I do something like this which works: </p>
  
  <pre><code>'image' =&gt; $block-&gt;image[0]-&gt;url,
  </code></pre>
  
  <p>I have tried similar code however it seems url is not available? </p>
  
  <pre><code>'downloadablePDF' =&gt; $block-&gt;downloadablePdf[0]
  </code></pre>
  
  <p>I need to return the URL so that I can pass the URL to a React Component to make the pdf downloadable. </p>
  
  <p>Here is my full code to fetch data from a matrix block: </p>
  
  <pre><code>function articles($array) {
      $articles = [];
      foreach ($array as $block) {
          switch ($block-&gt;type-&gt;handle) {
              case 'article':
                  $articles[] = [
                      'heading' =&gt; (string) $block-&gt;heading,
                      'subheading' =&gt; $block-&gt;subheading,
                      'text' =&gt; (string) $block-&gt;text,
                      'backgroundImage' =&gt; (isset($block-&gt;backgroundImage)) ? $block-&gt;backgroundImage[0]-&gt;url : '',
                      'downloadablePDF' =&gt; $block-&gt;downloadablePdf[0],
                      'videoUrl' =&gt; (isset($block-&gt;videoUrl)) ? $block-&gt;videoUrl : '',
                      'linkUrl' =&gt; (isset($block-&gt;linkUrl)) ? $block-&gt;linkUrl : '',
                      'size' =&gt; (isset($block-&gt;size)) ? $block-&gt;size-&gt;value : '',
                      'style' =&gt; (isset($block-&gt;style)) ? $block-&gt;style-&gt;value : '',
                      'verticalTextAlignment' =&gt; (isset($block-&gt;verticalTextAlignment)) ? $block-&gt;verticalTextAlignment-&gt;value : '',
                  ];
              break;
          }
      }
      return $articles;
  }
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-23T17:25:31.120Z
Comments.Text: 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
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-23T17:29:08.087Z
Comments.Text: @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
Comments.UserDisplayName: Filth
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-23T17:57:22.177Z
Comments.Text: 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
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-23T17:59:15.817Z
Comments.Text: I get the error: Internal Server Error
  Call to a member function getUrl() on null
Comments.UserDisplayName: Filth
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-23T18:01:20.627Z
Comments.Text: 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() : ''`
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-23T18:02:46.173Z
Comments.Text: Woohooo!! You really know your stuff @RobinSchambach thank you! Please post that above as an asnwer and I will mark it correct.
Comments.UserDisplayName: Filth
Tags: <assets><plugin-elementapi><pdf>
Title: Get PDF file from Assets Folder - Element API
ThreadText: # Get PDF file from Assets Folder - Element API
  
  (Asked by: username_0 on 2018-01-23)
  
  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;
  }
  
  ```
  
  **Tags:** assets, plugin-elementapi, pdf
  
  > **Comments:**
  > **username_1**: 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  
  > **username_0**: @username_1 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  
  > **username_1**: 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  
  > **username_0**: I get the error: Internal Server Error Call to a member function getUrl() on null  
  > **username_1**: 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() : ''`  
  > **username_0**: Woohooo!! You really know your stuff @username_1 thank you! Please post that above as an asnwer and I will mark it correct.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-01-23. Score: 3 | Accepted answer
  
  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
  
  
  ---
  
  