TransWikia.com

Problem with Grid fields?

ExpressionEngine® Answers Asked by developarvin on October 3, 2021

I have this kind of code structure. foo and bar are both Grid fields.

{if "{foo:total_rows}" > 0 OR "{bar:total_rows}" > 0}
  {if "{foo:total_rows}" > 0}
       {foo}
          {foo:a}
          {foo:b}
          {foo:c}
       {/foo}
  {/if}
{/if}

The problem is that {foo:a},{foo:b} and {foo:c} is not outputting anything.

However, when I do this

{if "{foo:total_rows}" > 0 OR "{bar:total_rows}" > 0}

   {foo}
      {foo:a}
      {foo:b}
      {foo:c}
   {/foo}

   {if "{foo:total_rows}" > 0}
        {foo}
           {foo:a}
           {foo:b}
           {foo:c}
        {/foo}
    {/if}
{/if}

I get

{foo}{/foo}abc

a, b, and c are the contents of {foo:a},{foo:b} and {foo:c} respectively.

What is the problem here? Is this a parse order issue?

3 Answers

I had some similar strangeness with conditionals and Grid fields. See Some crazy results from an "OR" conditional with Grid type

Answered by jphansen on October 3, 2021

This could be related to a bug that causes PREG_BACKTRACK_LIMIT_ERROR when parsing tag pairs. Essentially the regular expression thats used to find chunks of the template by pairing opening and closing tags doesn't perform efficiently enough in some cases. Here is the bug report.

The bug report contains a work around that may work for you. In system/expressionengine/libraries/api/Api_channel_fields.php change line 1852 (this line number is from the 2.9.2 codebase, so it may differ, should be within the get_pair_field function) to:

if (preg_match("/".LD."{$field_name}((?::[^s}]+)?)(s.*?)?".RD."(.*?)".LD.'/'."{$field_name}\1".RD."/s", $tagdata, $matches, 0, $offset)) 

Answered by Jonathan Dixon on October 3, 2021

Looks odd that {foo} is not recognised at first.

Have you tried modifying

{if "{foo:total_rows}" > 0}

It looks like you could be comparing a string to a number. What does

{if {foo:total_rows} > 0}

give you?

Also, what happens if you test removing the outer if?

Answered by dorothy Dorothy on October 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP