3

I have a matrix field with one block type. In that block type, I have one field that is a related entries field.

How do I access the title from the related entries field from the matrix field?

Here is the setup I have:

Entry name is programs In that entry I have a matrix block with handle name menuLinks In that block, the related entries field name is called menuLink.

Thanks

5

Related entries in a matrix field are technically related to the matrix block and not the entry. So you will first want to get the related entries via relatedTo(block); then loop through the related entries to get the field values.

{% for block in entry.menuLinks %}
    {% set relatedEntries = craft.entries.section('SectionName').relatedTo(block) %}
    {% for entry in relatedEntries %}
        <a href="{{ entry.url }}">{{ entry.title }}</a>
    {% endfor %}
{% endfor %}

If you know there is one and only one block or related entry then you can skip the for..in loop(s) and just use the .first() method (i.e. entry.menuLinks.first() and or craft.entries.section('SectionName').relatedTo(block).first());

Keep in mind that if you are just trying to create a simple list of links to related entries you do not need a matrix field at all (unless of course you are doing something more complicated). The Entries Field Type itself can support an unlimited number of related entries (and are sortable), so you could just create an entries field directly in programs.

  • Yes you're right, not sure why I complicated it. Making it just a related entries field worked perfect. Thank you! – user3818623 Nov 4 '14 at 13:30
  • Great! I thought you might be, but wasn't sure. As a side-note... make sure and delete the matrix field, and create a new field for the related entries vs just changing the field type to entries. If you change a field type from matrix to something else it will not delete the matrix tables in the database and may create orphaned records. If you already changed it then you might want to change it back to a matrix, then delete it (i.e. it must be a matrix field and have the correct handle for it to trigger the DB cleanup on delete). Then create your new entries field. – Douglas McDonald Nov 4 '14 at 15:22
  • wow - I hope you reported that little snivel for scheduled attention by the grues Inc.... – narration_sd Nov 4 '14 at 22:41
  • I did. They are aware, and helped me clean up my DB. I think a more permanent fix is in the works. – Douglas McDonald Nov 4 '14 at 22:48
  • @DouglasMcDonald What if in the reverse way? Use entry to located the related matrix block in other single. – Hs Tung Feb 4 '15 at 8:06

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.