I have 2 Matrix fields in an entry type, both of which use Dropdown fields:

Matrix: urls
    address (block type)
        context (dropdown field values include 'own')

Matrix: logos
   svg (block type)
        usage (dropdown field values include 'monoNeg')
        svg (source code)
   img (block type)
        usage (equivalent dropdown field)

Entries are assigned to a footer Globals field, from which I'm pairing a urls value with a logos value in a loop to write linked logos.

I can get the relevant URL easily enough with:

{% for org in footer.footerAccreditations %}
    {% set orgUrl = org.urls.context('own').one() %}
    {{ orgUrl.address }}
{% endfor %}

Which prints the address URL just fine.

But the same approach for the svg logo isn't working. Either one of the following returns 0, even when I know for sure that the org entry in question has a monoNeg SVG source.

{% set negLogoDirect = org.logos.usage('monoNeg').one() %}
{{ negLogoDirect|length }}

{% set negLogoTyped = org.logos.type('svg').usage('monoNeg').one() %}
{{ negLogoTyped|length }}

If I run through an all loop for the logos their usage reports exactly as I would expect:

{% set orgLogos = org.logos.all() %}

{% for logo in orgLogos %}
    {{ logo.usage }}
{% endfor %}

I assume I can make my template more verbose and get what I need, but I don't see why the same approach should work for one Matrix dropdown field and not for another.

Where am I going wrong?


That is most likely happening because usage is ambiguous for your logos field, so Craft isn’t sure which column to assign the condition to. If you give the usage field within your img block type a different handle, you should get reliable results.

  • Thanks, Brandon. That makes sense and gets a better outcome. Although, having done that, when I {% set negLogo = org.logos.usage('monoNeg').one() %} the returned results are the total number of rows in the field, but when I {% set negLogo = org.logos.usage('monoNeg').all() %} the returned results are 1, which is what I want, but the opposite of how I would expect to get there! – Jonathan Schofield Mar 19 '19 at 16:42
  • So, even though .all() is what returns a single result, I'm doing the following just in case it's ever more than 1: {% for logo in negLogos|slice(0) %}. – Jonathan Schofield Mar 19 '19 at 16: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.