Warnings in frontend if Switch is off or text field is empty in cloneable group

Support MB Group Warnings in frontend if Switch is off or text field is empty in cloneable groupResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #40164
    Daniele GladichDaniele Gladich
    Participant

    Hi, I'm using Metabox with Oxygen Builder. I've created a Custom Field Group for some pages.
    In these fields I have a cloneable group with several sub fields. One of these is a Switch that show or hide other sub fields.
    In Oxygen Builder I used a Code Block to show the cloneable group with html and php and it worked well.
    After switching to php 8, many warnings appear in frontend as these:

    Warning: Undefined array key "gara_contvar_btn" in .../wp-content/plugins/oxygen/component- framework/components/classes/code-block.class.php(133) : eval()'d code on line 9
    Warning: Undefined array key "gara_contvar_btntxt" in .../wp-content/plugins/oxygen/component- framework/components/classes/code-block.class.php(133) : eval()'d code on line 10
    Warning: Undefined array key "gara_contvar_btnurl" in .../wp-content/plugins/oxygen/component-framework/components/classes/code-block.class.php(133) : eval()'d code on line 11

    And these are the first rows in the code block:

    2 $group = rwmb_meta( 'gara_contvar' );
    3 foreach( $group as $value):
    ...
    9 $btn = $value['gara_contvar_btn']; // switch field
    10 $btn_txt = $value['gara_contvar_btntxt']; // text field without default value
    11 $btn_url = $value['gara_contvar_btnurl']; // url field without default value

    I saw that if the switch is Off, no value is set in the database, so the warning. And the same if I have a text or url field empty (the switch of line 9, hide or show the fileds of line 10 and 11).
    Can you help me to remove these warnings? Thanks
    Daniele

    #40169
    PeterPeter
    Moderator

    Hello,

    Actually, the empty value of a subfield will not be saved to the database. That's why you see the warning because there is no key (field ID) in the group if the value is empty. To fix this issue, please check the key is existed before assigning it to a variable. For example:

    if( array_key_exists( 'gara_contvar_btntxt', $value ) ) {
        $btn_txt = $value['gara_contvar_btntxt'];
    }
    #40171
    Daniele GladichDaniele Gladich
    Participant

    Thanks Peter, it works.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.