Support

Account

Home Forums Bug Reports has_sub_field() infinite loop

Solved

has_sub_field() infinite loop

  • I believe I’ve found an issue with has_sub_field() in conjunction with a repeater field nested inside a flex field. As this is the first time I’ve used Flex fields (and nested things like this), it’s possible this is user error. Here’s the code that causes the issue:

    
    $settings = array();
    while(has_sub_field('landing_page_content')):
      if(has_sub_field('settings')):
        while(has_sub_field('settings')):
          $settings['modal'] = strtolower(get_sub_field('modal'));
          $settings['link'] = get_sub_field('link');
          $settings['new_window'] = get_sub_field('new_window');
        endwhile;
      endif;
    [...]
    

    The issue arises when the layout being used by the flex field does not have a “settings” sub field – I would think that has_sub_field() would handle this check smoothly but instead the “landing_page_content” loop is immediately reset and has_sub_field(‘settings’) returns false. Anything after that point starts using the data from the original row in the landing_page_content flex field (until the next loop).

    I was able to work around this by implementing a counter and checking the row data directly for a Null setting’s field, replacing has_sub_field(). I think what is happening is that when has_sub_field() returns false it’s resetting the top loop instead of the inner loop (ie, it doesn’t realize that it is nested). If I understand correctly this is not the intended behavior.

    Here’s a var dump of the landing_page_content flex field, the issue appears in row 3 (row 4 is never shown):

    array(5) {
      [0]=>
      array(5) {
        ["acf_fc_layout"]=>
        string(16) "image_text_image"
        ["left_column"]=>
        string(4) "4469"
        ["middle_column"]=>
        string(192) "<strong>An eDiscovery-specific digest from the industry’s top thought leaders</strong>
    
    <ul>
    <li>Automatically Updated</li>
    <li>Easily Searchable</li>
    <li>Always Accessible</li>
    </ul>
    "
        ["right_column"]=>
        string(4) "4470"
        ["settings"]=>
        array(1) {
          [0]=>
          array(4) {
            [""]=>
            string(0) ""
            ["modal"]=>
            string(5) "Right"
            ["link"]=>
            string(0) ""
            ["new_window"]=>
            bool(true)
          }
        }
      }
      [1]=>
      array(5) {
        ["acf_fc_layout"]=>
        string(16) "image_text_image"
        ["left_column"]=>
        string(4) "4473"
        ["middle_column"]=>
        string(166) "<strong>Instant access to over 4000 eDiscovery experts</strong>
    
    <ul>
    <li>Ask a Question</li>
    <li>Start a Conversation</li>
    <li>Make New Connections</li>
    </ul>
    "
        ["right_column"]=>
        string(4) "4474"
        ["settings"]=>
        array(1) {
          [0]=>
          array(4) {
            [""]=>
            string(0) ""
            ["modal"]=>
            string(5) "Right"
            ["link"]=>
            string(0) ""
            ["new_window"]=>
            bool(true)
          }
        }
      }
      [2]=>
      array(5) {
        ["acf_fc_layout"]=>
        string(16) "image_text_image"
        ["left_column"]=>
        string(4) "4475"
        ["middle_column"]=>
        string(201) "<strong>Join Ben Bailey for an interactive mobile adventure</strong>
    
    <ul>
    <li>Includes General and eDiscovery Trivia</li>
    <li>Questions Updated Weekly</li>
    <li>Monthly Prize Drawings</li>
    </ul>
    "
        ["right_column"]=>
        string(4) "4476"
        ["settings"]=>
        array(1) {
          [0]=>
          array(4) {
            [""]=>
            string(0) ""
            ["modal"]=>
            string(7) "Neither"
            ["link"]=>
            string(0) ""
            ["new_window"]=>
            bool(true)
          }
        }
      }
      [3]=>
      array(4) {
        ["acf_fc_layout"]=>
        string(23) "textbox_textbox_textbox"
        ["left_column"]=>
        string(9) "1
    
    "
        ["middle_column"]=>
        string(9) "2
    
    "
        ["right_column"]=>
        string(9) "3
    
    "
      }
      [4]=>
      array(5) {
        ["acf_fc_layout"]=>
        string(16) "image_text_image"
        ["left_column"]=>
        string(4) "4477"
        ["middle_column"]=>
        string(164) "<strong>A comprehensive set of action-oriented eDiscovery tools</strong>
    
    <ul>
    <li>Resources</li>
    <li>Reference Materials</li>
    <li>Upcoming Events</li>
    </ul>
    "
        ["right_column"]=>
        string(4) "4478"
        ["settings"]=>
        array(1) {
          [0]=>
          array(4) {
            [""]=>
            string(0) ""
            ["modal"]=>
            string(5) "Right"
            ["link"]=>
            string(0) ""
            ["new_window"]=>
            bool(true)
          }
        }
      }
    }
  • Hi @KMDG

    The has_sub_field function is not to be used within an if statement, only in a while loop.

    You will need to use get_sub_field to get_field to check if the repeater / flex field contains any data.

    Please refer to the code examples for these fields.

    Thanks
    E

  • Ah, I did actually look at those but must have had a mental hiccup – I could have sworn it was using has_sub_field() to check for a field’s presence.

    Problem solved, not a bug.

    Thanks!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘has_sub_field() infinite loop’ is closed to new replies.