1

I have a Matrix field where one of my child fields is a Dynamic Dropdown that uses the following code to pull in entries:

{% for entry in craft.entries.limit(null).section('dealers').type('online') %}
  { "value":"{{ entry.id }}" , "label":"{{ entry | raw }}"
    {% if entry.uri == '__home__' %} , "default":true{% endif %}} 
    {% if not loop.last %},{% endif %}
{% endfor %}

The Matrix field has a handle of mensPDPLinks and the child field has a handle of retailer.

The above code creates a dropdown containing a list of retailers from another section.

When editing an entry containing this field a person can add as many retailers as needed by adding additional Matrix rows. It should be storing the entryID for each of the retailer entries they select. There are additional Matrix child fields to include other data to each retailer they select.

In my templates I'd like to loop through the entries (retailers) they've added in the Matrix field and display some data from the retailers entry.

Can someone point me in a direction? I'm kinda lost as to how I can do this.

2

You can loop though your "myMatrixBlockName" blocks to get all the retailers' entry IDs. Then use that array of IDs in a craft.entries ElementCriteriaModel to get the entries you're after. Use fixedOrder as a parameter to maintain the Matrix's sort order if you want that.

{% set retailerIds = [] %}

{# Loop `myMatrixBlockName` blocks to get the retailers' entry IDs #}
{% for block in entry.mensPDPLinks.type('myMatrixBlockName') %}

    {% set retailerId = block.retailer %}
    {% set retailerIds = retailerIds|merge([retailerId]) %}

{% endfor %}

{# Convert `retailerIds` array into comma separated list #}
{% set retailerIdString = retailerIds|join(', ') %}

{# Get `retailers` in the same order as listed in the Matrix field #}
{% set retailers = craft.entries.id(retailerIdString).fixedOrder %}

{# Loop `retailers` #}
{% for retailer in retailers %}

    {{ retailer.title }}

{% endfor %}
| improve this answer | |
  • Great, thanks. I had to change it slightly to make it work. Namely, converting the array to a comma-separated string. After the for loop I did: {% set retailerIdString = retailerIds | join(', ') %} Then passed that to craft.entries. Thanks for the help. – Brett DeWoody Oct 29 '14 at 16:55
  • Yep ok, didn't test the code at all. Good that it works for you now, will update the code. Thanks for the feedback, @Brett! – carlcs Oct 29 '14 at 16:58

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.