<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>

CC BY-SA 3.0
2015-02-07T17:57:17.810Z
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".
Jeremiah
CC BY-SA 3.0
2015-02-07T18:01:36.957Z
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 ;)
carlcs
CC BY-SA 3.0
2015-02-07T18:05:12.173Z
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'.
carlcs
CC BY-SA 3.0
2015-02-07T18:10:59.610Z
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).
carlcs
CC BY-SA 3.0
2015-02-07T18:32:52.273Z
A plain text field works fine, so I know the matrix field is doing its job at least for text...
Jeremiah
CC BY-SA 3.0
2015-02-08T03:36:53.383Z
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.
Jeremiah
CC BY-SA 3.0
2015-02-08T03:48:53.907Z
Now it's 'widgetFile' for your Entries Field handle, not 'widgetPrimary' any more, @Jeremiah? Good to see you got this sorted.
carlcs
CC BY-SA 3.0
2015-02-07T17:43:00.597Z
2015-02-07T17:55:29.717Z
2017-04-13T12:51:00.200Z
carlcs
<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>

CC BY-SA 3.0
2015-02-09T08:04:45.317Z
So it was in fact what I posted in my answer, you used the wrong handle in you template for the Entries Field.
carlcs
CC BY-SA 3.0
2015-02-09T09:56:31.913Z
Yep! That helped me sort it out. Much appreciated!
Jeremiah
CC BY-SA 3.0
2015-02-09T02:55:16.630Z
2015-02-09T02:55:16.630Z
Jeremiah
<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>

CC BY-SA 3.0
2015-02-07T17:23:46.273Z
Thank you for the reply! Unfortunately I am still receiving the same error. At the banging my head against the wall stage. lol
Jeremiah
CC BY-SA 3.0
2015-02-07T17:38:10.303Z
Sounds like widgetPrimary is not defined. Can you explain what 'widgetPrimary' and 'widgets' are?
Douglas McDonald
CC BY-SA 3.0
2015-02-07T18:02:20.960Z
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
Jeremiah
CC BY-SA 3.0
2015-02-07T17:01:59.777Z
2015-02-07T17:01:59.777Z
Douglas McDonald
<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>

CC BY-SA 3.0
2015-02-08T22:21:09.923Z
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.)
Lindsey D
CC BY-SA 3.0
2015-02-09T09:55:44.067Z
Thanks for the tip! Somehow I simply missed this, my bad. Edited now. And thank you all for your help, btw!
Jeremiah
<matrix><relations><files>
Retrieve Asset Filename from an entry, inside a matrix field
# 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 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, @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 ;)  
> **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, @Jeremiah? 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  

---

