1

I have an optional matrix field for a page banner in one of my channels. If someone publishes a new channel entry or edits one where the banner matrix field has never been touched, then the conditional {if banner_matrix_field}banner matrix field has content{/if} validates as true, even though it's empty.

For example, if I publish a new page, and leave the banner matrix field empty, then the class graphic will still display in my template:

{exp:channel:entries
    channel='test'
}
<header class="banner clearfix{if banner_matrix_field} graphic{/if}">
    {!-- Banner Content --}
</header><!--/.banner-->
{/exp:channel:entries}

If I add a row to the matrix field then delete it, then the field correctly validates as empty.

UPDATE: The add/delete row workaround actually only works every time you edit the entry. So if I edit the entry once, add/delete a matrix row, save, then come back and edit the same entry again and save without adding/deleting the matrix row, the conditional validates as true again.

It doesn't seem to matter what kind of fields are inside the matrix cells, I've tried it with Assets, File, and the default plain text cells with the same result.

  • EE 2.6.1
  • Matrix 2.5.5
1
  • 2
    Hey Kristen, I added this to our bug tracker. We'll be doing a 2.5.6 release sometime soon - maybe next week, if timing allows, and this will be fixed in that release. Commented Jun 6, 2013 at 5:42

1 Answer 1

5

You can use

{if banner_matrix_field:total_rows >= 1} graphic{/if}

Matrix docs link

5
  • Well sure, I can, but that doesn't really fix the problem.
    – kmgdev
    Commented Jun 4, 2013 at 20:49
  • 1
    Why doesn't it fix the problem, did you try it? Your 'graphic' class shouldn't display if you use the above test for rows. Commented Jun 4, 2013 at 22:41
  • 1
    @romans-8-31-39 It's a workaround rather than a fix. The main issue is that my conditional should work but doesn't, indicating a problem with Matrix. The other issue with your solution is it turns a simple conditional into an advanced conditional, which has performance and parsing implications. Don't get me wrong, it's a good suggestion, it's just not an actual fix.
    – kmgdev
    Commented Jun 5, 2013 at 23:21
  • 1
    @kgrote, excuse me, but the use ":total_rows" doesn't make the conditional advanced. Actually there's no difference between simple and advanced conditionals if the conditional is evaluated by a module tag. Take a look at slides 67-87: slideshare.net/lodewijkschutte/parse-order-pro
    – Sobral
    Commented Jun 6, 2013 at 0:21
  • 1
    After speaking with P&T support, apparently this is the way to go. Apparently matrix can't ever validate as totally empty in order to be compatible with EE's revision tracker. I'd make one modification, and change the tag to {if matrix_field:total_rows > 0}, just to make it that much more svelte.
    – kmgdev
    Commented Jul 2, 2013 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.