1

If I have a matrix field with 2 fields "Name" and "Entry" is it possible to lookup the first row in that matrix field which has a specific "Name"?

Context: Twig template.

3

According to this answer the only ways to filter matrix blocks are by type, or by position. So you need to use the if condition on the twig for tag.

{% for m in entry.myMatrix if m.theName == nameImLookingFor %}
  {% if loop.index == 1 %}
    got the first one, it is {{m.theName}}
  {% endif %}
{% endfor %}

If you don't want to loop over all of the blocks, you can also install the mnbreakandcontinue plugin and break out of the loop after the first match:

{% for m in entry.myMatrix if m.theName == nameImLookingFor %}
  got the first one, it is {{m.theName}}
  {% break %}
{% endfor %}
  • And according to Brandon's comment here, things will change with Craft 2.3! Really good news... – carlcs Nov 27 '14 at 19:36
  • Awesome thanks! and thanks for mentioning the 'mnbreakandcontinue' plugin :) – Lea Hayes Nov 27 '14 at 19:57

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.