Field conditional logic doesn't work with Gutenberg

Support General Field conditional logic doesn't work with GutenbergResolved

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #18362
    YumikomYumikom
    Participant

    Hi,

    I created a Post field and set the Conditional Logic in Advanced as below.

    • Visible All
    • post_type = page

    However, when Gutenberg is enabled, conditional logic does not work.
    This field has been displayed for all post types.

    add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );
    
    function your_prefix_register_meta_boxes( $meta_boxes ) {
        $prefix = '';
    
        $meta_boxes[] = array (
            'title' => esc_html__( 'test', 'text-domain' ),
            'id' => 'test',
            'post_types' => array(
                0 => 'post',
                1 => 'general',
            ),
            'context' => 'normal',
            'priority' => 'high',
            'fields' => array(
                array (
                    'id' => $prefix . 'post_vw01o0dnsbe',
                    'type' => 'post',
                    'name' => esc_html__( 'Post', 'text-domain' ),
                    'post_type' => array(
                        0 => 'post',
                    ),
                    'field_type' => 'select_advanced',
                    'visible' => array(
                        'when' => array(
                            array (
                                0 => 'post_type',
                                1 => '=',
                                2 => 'page',
                            ),
                        ),
                        'relation' => 'and',
                    ),
                ),
            ),
        );
    
        return $meta_boxes;
    }
    
    #18374
    Anh TranAnh Tran
    Keymaster

    Hi Yumikom,

    I see you set a condition to show a field based on post_type. Unfortunately, the plugin doesn't support that kind of condition. You can change the visibility of a field or a meta box based on a value of another field or another input.

    #18379
    YumikomYumikom
    Participant

    Hi,

    I'm using Metabox for many years. A license is also bought every client.

    This condition was used so far, and it was no problem. It can't be used in Gutenberg.
    Please support post_type as a field condition even for posts using Gutenberg.
    I'm having trouble getting Metabox in the classic editor I've used up to now to be able to migrate to Gutenberg.

    Regards,
    Yumiko

    #18398
    Anh TranAnh Tran
    Keymaster

    Hi Yumiko,

    Thanks a lot for being our customer for years! I really appreciate that and your support!

    Regarding this issue: a problem here is you set the meta box to appear to only 2 post types post and general. So, when you set the conditional logic for post type = page, it can't work (because the meta box is not loaded for that post type). Can you please change the post_types parameter of the meta box to include page and try again? I've checked and seen that it supports post_type rule.

    #18408
    YumikomYumikom
    Participant

    Hi Anh,

    I'm always thankful for your support.
    I was relieved to support post_type rule.
    I use this condition a lot.

    As you suggested, I tried with simple settings.
    But again, the condition is not working in Gutenberg, which is fine in the classic editor.

    With the following settings, I want this field to be displayed only when post_type is 'page', but it will be displayed in 'post', 'page' and 'general'.

    
    add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );
    
    function your_prefix_register_meta_boxes( $meta_boxes ) {
        $prefix = '';
    
        $meta_boxes[] = array (
            'title' => esc_html__( 'test', 'text-domain' ),
            'id' => 'test',
            'post_types' => array(
                0 => 'post',
                1 => 'page',
                2 => 'general',
            ),
            'context' => 'normal',
            'priority' => 'high',
            'fields' => array(
                array (
                    'id' => $prefix . 'post_mnfi92e2ko',
                    'type' => 'post',
                    'name' => esc_html__( 'Post', 'text-domain' ),
                    'post_type' => array(
                        0 => 'page',
                    ),
                    'field_type' => 'select_advanced',
                    'visible' => array(
                        'when' => array(
                            array (
                                0 => 'post_type',
                                1 => '=',
                                2 => 'page',
                            ),
                        ),
                        'relation' => 'and',
                    ),
                ),
            ),
        );
    
        return $meta_boxes;
    }
    

    Thanks and Regards,
    Yumiko

    #18432
    Anh TranAnh Tran
    Keymaster

    Hi Yumiko,

    I found the problem. The post_type is not a Meta Box field, so using it the normal way doesn't work. You need to use outside conditions.

    Change your code to:

    add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
        $prefix = '';
    
        $meta_boxes[] = array (
            'title' => esc_html__( 'test', 'text-domain' ),
            'id' => 'test',
            'post_types' => array(
                0 => 'post',
                1 => 'page',
                2 => 'general',
            ),
            'context' => 'normal',
            'priority' => 'high',
            'fields' => array(
                array (
                    'id' => $prefix . 'post_mnfi92e2ko',
                    'type' => 'post',
                    'name' => esc_html__( 'Post', 'text-domain' ),
                    'post_type' => 'page',
                    'field_type' => 'select_advanced',
                    'class' => 'your-custom-class',
                ),
            ),
        );
    
        return $meta_boxes;
    } );
    

    Note that I added a your-custom-class to set a CSS class for your field. And then add the following code to your theme's functions.php file (or using a functionality/Code Snippet plugin):

    add_filter( 'rwmb_outside_conditions', function( $conditions ) {
        $conditions['.your-custom-class'] = [
            'visible' => ['post_type', 'page'],
        ];
        return $conditions;
    } );
    #18453
    YumikomYumikom
    Participant

    Hi Anh,

    Thank you for your teaching about Outside condition.
    I solved what I wanted to do!

    Thanks!

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