Answers.Body: <p>I would first check if you actually set up the Field handles you are currently using in your template:</p>
  
  <p>Matrix Field handle: 'contentPrimary'<br>
  Block Type handle: 'widget'<br>
  Entries Field handle: 'widgetPrimary'<br>
  Assets Field handle: 'widgets'  </p>
  
  <p>Then try to use the <code>first()</code> method instead of direct access to the array.
  <s>Or use direct access only after adding find() method that creates an array out of your ElementCriteriaModel.</s> <em>Edit:</em> Direct access like this: <code>myArray[0]</code> should also work with an ElementCriteriaModel, but throws errors if no first elements exists (→ read <a href="https://craftcms.stackexchange.com/a/296/125">Brandon's answer on this topic</a> for more info).</p>
  
  <pre><code>{% for block in entry.contentPrimary %}
      {% switch block.type %}
          {% case "widget" %}
              {% set relatedEntry = block.widgetPrimary.first() %}
              {% if relatedEntry %}
                  {% set asset = relatedEntry.widgets.first() %}
                  {% if asset %}
                      {{ asset.title }}
                  {% endif %}
              {% endif %}
      {% endswitch %}
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-07T17:57:17.810Z
Answers.Text: So strange, all 4 criteria for the field handles match up. I tried the revised code you just posted and it returns the following still:
  
      Internal Server Error
  
      Craft\MatrixBlockModel and its behaviors do not have a method or closure named "widgetPrimary".
Answers.UserDisplayName: Jeremiah
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-07T18:01:36.957Z
Answers.Text: It's a sign, @Jeremiah. Your Craft site wants you to not use this strange Assets / EntriesField setup only to load template partials. Just do yourself a favor and refactor this whole thing ;)
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-07T18:05:12.173Z
Answers.Text: But to stay on topic, triple check if the field handle of your Entries Field is 'widgetPrimary' and if this field is really available in your Matrix Block Type 'widget'.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-07T18:10:59.610Z
Answers.Text: Do one more thing: add a simple Plain Text Field to your 'widget' Matrix Block Type and see if this works (remove the 'widgetPrimary' field's part from the template for this).
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-07T18:32:52.273Z
Answers.Text: A plain text field works fine, so I know the matrix field is doing its job at least for text...
Answers.UserDisplayName: Jeremiah
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-08T03:36:53.383Z
Answers.Text: I should mention that the following code works for the text section in my Matrix field. Additionally, the last line {{ block.widgetFile[0] }} shows the TITLE of the linked entry, {{ block.widgetFile[0].id }} shows the ID of the linked entry. Here is the code:
  
      {% for block in entry.contentPrimary %}
             {% switch block.type %}
           {% case "text" %}
            {{ block.text }}
           {% case "widget" %}
            {{ block.widgetFile[0] }}
  
  So inside the entry pulled from widgetFile, I have an asset field from which I need to pull the filename.
Answers.UserDisplayName: Jeremiah
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-08T03:48:53.907Z
Answers.Text: Now it's 'widgetFile' for your Entries Field handle, not 'widgetPrimary' any more, @Jeremiah? Good to see you got this sorted.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-07T17:43:00.597Z
Answers.LastActivityDate: 2015-02-07T17:55:29.717Z
Answers.LastEditDate: 2017-04-13T12:51:00.200Z
Answers.UserDisplayName: carlcs
Answers.Body: <p>Here is the solution I came up with. Hope this can help someone else. Thank you all for your suggestions, too!</p>
  
  <pre><code>{% switch block.type %}
      {% case "text" %}
          {{ block.text }}
      {% case "widget" %}
          {% include "widgets/primary/"~ block.widgetFile[0].widgetPrimary[0].filename ~"" %}
  {% endswitch %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-09T08:04:45.317Z
Answers.Text: So it was in fact what I posted in my answer, you used the wrong handle in you template for the Entries Field.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-09T09:56:31.913Z
Answers.Text: Yep! That helped me sort it out. Much appreciated!
Answers.UserDisplayName: Jeremiah
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-09T02:55:16.630Z
Answers.LastActivityDate: 2015-02-09T02:55:16.630Z
Answers.UserDisplayName: Jeremiah
Answers.Body: <p>You're problem is with 'block.entry[0].widgets', which I suspect is a typo. Try this.</p>
  
  <pre><code>{% case "widget" %}
      {% if block.widgetPrimary|length %}
          {% if block.widgetPrimary[0].widgets|length %}
              {{ block.widgetPrimary[0].widgets[0].filename }}
          {% endif %}
      {% endif %}
  </code></pre>
  
  <p>Also, be careful of combining conditionals where the second will throw an error if the first fails. (i.e. in the following <code>block.widgetPrimary[0].widgets</code> will throw an error if <code>block.widgetPrimary|length</code> fails).</p>
  
  <pre><code>{% if block.widgetPrimary|length and block.widgetPrimary[0].widgets|length %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-07T17:23:46.273Z
Answers.Text: Thank you for the reply! Unfortunately I am still receiving the same error. At the banging my head against the wall stage. lol
Answers.UserDisplayName: Jeremiah
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-07T17:38:10.303Z
Answers.Text: Sounds like widgetPrimary is not defined. Can you explain what 'widgetPrimary' and 'widgets' are?
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-07T18:02:20.960Z
Answers.Text: widgetPrimary is defined and I just double-checked for typos. For any "primary widgets", I create an entry in this section. So in this case it is called Primary Widget, with the handle being widgetPrimary. This selects from a specific upload folder in my templates location (widgets/primary)
  
  Widget is my Block Type handle
  Widgets is my Assets Field handle
Answers.UserDisplayName: Jeremiah
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-07T17:01:59.777Z
Answers.LastActivityDate: 2015-02-07T17:01:59.777Z
Answers.UserDisplayName: Douglas McDonald
Body: <p>I must simply be blanking this morning, but for whatever reason, this has me stumped... I have a Structure section I use for the different sections of my site which sets titles for each page, etc.</p>
  
  <p>Additionally, I have another section called "Widgets". These widgets are stored in the template folder and each widget is linked up to a dedicated entry in an Asset field. Finally, each Structure entry (take the home page for example) has a Matrix field. This allows me to rapidly add new pages from templates I have already built. Unfortunately this time around I am having trouble pulling the filename for the widgets, though I seem to be able to grab the asset IDs. Following is the code. Thank you in advance for the help; I am sure it is simple but I've been banging my head against my desk for a little too long now...</p>
  
  <pre><code>                {% block contentPrimary %}
                  {% switch entry.type %}
                      {% case "mainContentLeft" %}
                          {% for block in entry.contentPrimary %}
                              {% switch block.type %}
                                  {% case "text" %}
                                      {{ block.text }}
                                  {% case "widget" %}
                                      {% if block.widgetPrimary|length and block.entry[0].widgets|length %}
                                          {{ block.widgetPrimary[0].widgets[0].filename }}
                                      {% endif %} 
                              {% endswitch %}
                          {% endfor %}
                      {% case "mainContentRight" %}   
                          Main Content Right
                      {% case "singleColumn" %}   
                          Single Column
                  {% endswitch %}
              {% endblock contentPrimary %}
  </code></pre>
  
  <p>After trying a few different things, I've seen a couple different errors, but the error I see at present states:</p>
  
  <blockquote>
    <p>Craft\MatrixBlockModel and its behaviors do not have a method or closure named "widgetPrimary".</p>
  </blockquote>
  
  <p>Thank you again, in advance!</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-08T22:21:09.923Z
Comments.Text: If you've found an answer to your own question, I highly recommend posting it as an official **Answer**. You can then mark your own answer as the correct one. (Jamming your solution into the beginning of your question is extremely confusing.)
Comments.UserDisplayName: Lindsey D
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-09T09:55:44.067Z
Comments.Text: Thanks for the tip! Somehow I simply missed this, my bad. Edited now. And thank you all for your help, btw!
Comments.UserDisplayName: Jeremiah
Tags: <matrix><relations><files>
Title: Retrieve Asset Filename from an entry, inside a matrix field
ThreadText: # Retrieve Asset Filename from an entry, inside a matrix field
  
  (Asked by: username_0 on 2015-02-07)
  
  I must simply be blanking this morning, but for whatever reason, this has me stumped... I have a Structure section I use for the different sections of my site which sets titles for each page, etc.
  
  Additionally, I have another section called "Widgets". These widgets are stored in the template folder and each widget is linked up to a dedicated entry in an Asset field. Finally, each Structure entry (take the home page for example) has a Matrix field. This allows me to rapidly add new pages from templates I have already built. Unfortunately this time around I am having trouble pulling the filename for the widgets, though I seem to be able to grab the asset IDs. Following is the code. Thank you in advance for the help; I am sure it is simple but I've been banging my head against my desk for a little too long now...
  
  ```
                  {% block contentPrimary %}
                  {% switch entry.type %}
                      {% case "mainContentLeft" %}
                          {% for block in entry.contentPrimary %}
                              {% switch block.type %}
                                  {% case "text" %}
                                      {{ block.text }}
                                  {% case "widget" %}
                                      {% if block.widgetPrimary|length and block.entry[0].widgets|length %}
                                          {{ block.widgetPrimary[0].widgets[0].filename }}
                                      {% endif %} 
                              {% endswitch %}
                          {% endfor %}
                      {% case "mainContentRight" %}   
                          Main Content Right
                      {% case "singleColumn" %}   
                          Single Column
                  {% endswitch %}
              {% endblock contentPrimary %}
  
  ```
  
  After trying a few different things, I've seen a couple different errors, but the error I see at present states:
  
  > Craft\MatrixBlockModel and its behaviors do not have a method or closure named "widgetPrimary".
  
  Thank you again, in advance!
  
  **Tags:** matrix, relations, files
  
  > **Comments:**
  > **username_1**: If you've found an answer to your own question, I highly recommend posting it as an official **Answer**. You can then mark your own answer as the correct one. (Jamming your solution into the beginning of your question is extremely confusing.)  
  > **username_0**: Thanks for the tip! Somehow I simply missed this, my bad. Edited now. And thank you all for your help, btw!  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_2 on 2015-02-07. Score: 3
  
  I would first check if you actually set up the Field handles you are currently using in your template:
  
  Matrix Field handle: 'contentPrimary'  
  Block Type handle: 'widget'  
  Entries Field handle: 'widgetPrimary'  
  Assets Field handle: 'widgets'
  
  Then try to use the `first()` method instead of direct access to the array.
  ~~Or use direct access only after adding find() method that creates an array out of your ElementCriteriaModel.~~ *Edit:* Direct access like this: `myArray[0]` should also work with an ElementCriteriaModel, but throws errors if no first elements exists (→ read [Brandon's answer on this topic](https://craftcms.stackexchange.com/a/296/125) for more info).
  
  ```
  {% for block in entry.contentPrimary %}
      {% switch block.type %}
          {% case "widget" %}
              {% set relatedEntry = block.widgetPrimary.first() %}
              {% if relatedEntry %}
                  {% set asset = relatedEntry.widgets.first() %}
                  {% if asset %}
                      {{ asset.title }}
                  {% endif %}
              {% endif %}
      {% endswitch %}
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: So strange, all 4 criteria for the field handles match up. I tried the revised code you just posted and it returns the following still:      Internal Server Error      Craft\MatrixBlockModel and its behaviors do not have a method or closure named "widgetPrimary".  
  > **username_2**: It's a sign, @username_0. Your Craft site wants you to not use this strange Assets / EntriesField setup only to load template partials. Just do yourself a favor and refactor this whole thing ;)  
  > **username_2**: But to stay on topic, triple check if the field handle of your Entries Field is 'widgetPrimary' and if this field is really available in your Matrix Block Type 'widget'.  
  > **username_2**: Do one more thing: add a simple Plain Text Field to your 'widget' Matrix Block Type and see if this works (remove the 'widgetPrimary' field's part from the template for this).  
  > **username_0**: A plain text field works fine, so I know the matrix field is doing its job at least for text...  
  > **username_0**: I should mention that the following code works for the text section in my Matrix field. Additionally, the last line {{ block.widgetFile[0] }} shows the TITLE of the linked entry, {{ block.widgetFile[0].id }} shows the ID of the linked entry. Here is the code:      {% for block in entry.contentPrimary %}            {% switch block.type %}          {% case "text" %}           {{ block.text }}          {% case "widget" %}           {{ block.widgetFile[0] }}  So inside the entry pulled from widgetFile, I have an asset field from which I need to pull the filename.  
  > **username_2**: Now it's 'widgetFile' for your Entries Field handle, not 'widgetPrimary' any more, @username_0? Good to see you got this sorted.  
  
  ---
  
  ### Answer by username_0 on 2015-02-09. Score: 1
  
  Here is the solution I came up with. Hope this can help someone else. Thank you all for your suggestions, too!
  
  ```
  {% switch block.type %}
      {% case "text" %}
          {{ block.text }}
      {% case "widget" %}
          {% include "widgets/primary/"~ block.widgetFile[0].widgetPrimary[0].filename ~"" %}
  {% endswitch %}
  
  ```
  
  > **Comments:**
  > **username_2**: So it was in fact what I posted in my answer, you used the wrong handle in you template for the Entries Field.  
  > **username_0**: Yep! That helped me sort it out. Much appreciated!  
  
  ---
  
  ### Answer by username_3 on 2015-02-07. Score: 1
  
  You're problem is with 'block.entry[0].widgets', which I suspect is a typo. Try this.
  
  ```
  {% case "widget" %}
      {% if block.widgetPrimary|length %}
          {% if block.widgetPrimary[0].widgets|length %}
              {{ block.widgetPrimary[0].widgets[0].filename }}
          {% endif %}
      {% endif %}
  
  ```
  
  Also, be careful of combining conditionals where the second will throw an error if the first fails. (i.e. in the following `block.widgetPrimary[0].widgets` will throw an error if `block.widgetPrimary|length` fails).
  
  ```
  {% if block.widgetPrimary|length and block.widgetPrimary[0].widgets|length %}
  
  ```
  
  > **Comments:**
  > **username_0**: Thank you for the reply! Unfortunately I am still receiving the same error. At the banging my head against the wall stage. lol  
  > **username_3**: Sounds like widgetPrimary is not defined. Can you explain what 'widgetPrimary' and 'widgets' are?  
  > **username_0**: widgetPrimary is defined and I just double-checked for typos. For any "primary widgets", I create an entry in this section. So in this case it is called Primary Widget, with the handle being widgetPrimary. This selects from a specific upload folder in my templates location (widgets/primary)  Widget is my Block Type handle Widgets is my Assets Field handle  
  
  ---
  
  