1

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!

  • 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 Feb 8 '15 at 22:21
  • Thanks for the tip! Somehow I simply missed this, my bad. Edited now. And thank you all for your help, btw! – Jeremiah Feb 9 '15 at 9:55
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 %}
  • 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 Feb 7 '15 at 17:57
  • 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 Feb 7 '15 at 18:01
  • 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 Feb 7 '15 at 18:05
  • 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 Feb 7 '15 at 18:10
  • A plain text field works fine, so I know the matrix field is doing its job at least for text... – Jeremiah Feb 7 '15 at 18:32
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 %}
  • Thank you for the reply! Unfortunately I am still receiving the same error. At the banging my head against the wall stage. lol – Jeremiah Feb 7 '15 at 17:23
  • Sounds like widgetPrimary is not defined. Can you explain what 'widgetPrimary' and 'widgets' are? – Douglas McDonald Feb 7 '15 at 17:38
  • 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 Feb 7 '15 at 18:02
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 %}
  • So it was in fact what I posted in my answer, you used the wrong handle in you template for the Entries Field. – carlcs Feb 9 '15 at 8:04
  • Yep! That helped me sort it out. Much appreciated! – Jeremiah Feb 9 '15 at 9:56

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.