Support

Account

Home Forums Add-ons Options Page Dynamically add message based on another field value

Solving

Dynamically add message based on another field value

  • Hi,

    I am trying to dynamically populate a Message field based on another field in a Repeater.
    But I always get white screen with my code, the_row(): is added.
    The debug info says
    PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes) in /home/xxxx/webapps/xxx/wp-includes/class-wp-list-util.php on line 134

    This works:

    function cban_shortcode_disable_field($field) {
        $field['disabled'] = false;
        //$field['readonly'] = true;
        $field['message'] = 'ALMA';
    
        print_r($field);
    
        return $field;
    }
    add_filter('acf/load_field/name=canb_shortcode', 'cban_shortcode_disable_field',20,1);

    But if I try to load the message dynamically I got white screen:

    add_filter('acf/load_field/name=canb_shortcode', 'canb_shortcode_load_field');
    
    function canb_shortcode_load_field($field){
    
        
        // if has rows
        if( have_rows('canb_notification_bars', 'option') ) :
            // while has rows
            while( have_rows('canb_notification_bars', 'option') ) : the_row();
                
                /* Get the name from the Notification bar name field */
                $canb_name = get_sub_field('canb_notification_bar_name','option');
                $canb_name = sanitize_title_with_dashes( $canb_name );
                
                $field['message'] = $canb_name;
    
            endwhile;
    
        endif;
    
    return $field;
    
    }

    Can someone point me in the right direction?

    I have tried to use Example 2 from this page:
    https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/

    I want to create a small plugin and in the message I would like to display a shortcode variable which the user can use to embed the notification bar.

    Thanks.
    Regards,
    Peter.

  • remove ‘option’ from here
    this

    
    $canb_name = get_sub_field('canb_notification_bar_name','option');
    

    should be

    
    $canb_name = get_sub_field('canb_notification_bar_name');
    

    I’m not sure that is causing your error, but that’s the only thing I can see wrong. For some reason your code is causing an infinite loop.

    Also, you should be using the acf/prepare_field hook to modify the field message. While acf/load_field can be used here it can have unintended side effects.

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

You must be logged in to reply to this topic.