htmlburger / carbon-fields

WordPress Custom Fields Library ✨
https://carbonfields.net/
Other
1.36k stars 245 forks source link

Conditional logic not working with reusable blocks #953

Open Ryvix opened 3 years ago

Ryvix commented 3 years ago

Version

Expected Behavior

Conditional field should remain hidden when setting as a reusable block.

Actual Behavior

Hidden fields are no longer hidden when using a reusable block.

Container definition

CFBlock::make( __( 'Example' ) )
       ->set_icon( 'grid-view' )
       ->add_fields( array(

           Field::make( 'select', 'example1', __( 'Example1', 'example' ) )
                ->set_default_value( '1' )
                ->set_options( array(
                    '1' => '1',
                    '2' => '2',
                ) )
                ->set_help_text( __( 'Example1.', 'example1' ) ),

           Field::make( 'select', 'example2', __( 'Example2', 'example' ) )
                ->set_default_value( '1' )
                ->set_options( array(
                    '1' => '1',
                    '2' => '2',
                ) )
                ->set_help_text( __( 'Example2.', 'example' ) )
                ->set_conditional_logic( array(
                    'relation' => 'AND',
                    array(
                        'field'   => 'example1',
                        'value'   => '2',
                        'compare' => '=',
                    )
                ) )
                ->set_help_text( __( 'Example2.', 'example' ) ),

       ) )
       ->set_render_callback( function ( $fields, $attributes, $inner_blocks ) {
       } );

Steps to Reproduce the Problem

  1. Add Example block
  2. Set block as reuasable
  3. Click Save

Comments

I don't know why this would be an intended behaviour so I figure it must be a bug.

May be related to issue #800

TobiasPrt commented 3 years ago

I have the same problem within complex fields within a block. Have you found a solution to this?

Ryvix commented 3 years ago

I don't believe I have. Although I haven't tested it lately to find out if it was fixed in the latest update but sounds like it isn't.

ChrissiQ commented 3 years ago

Using Carbon Fields latest v3.3.0, conditional logic does seem to work 90% for me but there is an error being thrown and page functionality breaking when deleting the block.

Sample code:

Block::make(__('Testing Block'))
    ->add_fields([
        Field::make('text', 'title', __('Title')),
        Field::make('checkbox', 'option', __('Option?')),
        Field::make('text', 'content', __('Text Content'))
            ->set_conditional_logic( array(
                array(
                    'field' => 'option',
                    'value' => true,
                )
        ) ),
    ])
    ->set_render_callback(function() {});

When using this sample code, I am able to create the block and use it, and conditional logic works normally. However, when I delete the block, this error is thrown and the page does not update properly after the block has been deleted:

Screen Shot 2021-04-07 at 11 31 30 AM

blocks.min.js?ver=3.3.0:11 Uncaught (in promise) TypeError: Cannot read property 'attributes' of null
    at blocks.min.js?ver=3.3.0:11
    at blocks.min.js?ver=3.3.0:11
    at blocks.min.js?ver=3.3.0:11
    at core.min.js?ver=3.3.0:47
    at data.min.js?ver=943087ae96d075f126df689839bb96b9:2
    at Array.forEach (<anonymous>)
    at i (data.min.js?ver=943087ae96d075f126df689839bb96b9:2)
    at data.min.js?ver=943087ae96d075f126df689839bb96b9:2
    at p (data.min.js?ver=943087ae96d075f126df689839bb96b9:2)
    at redux-routine.min.js?ver=284288e957394d2097c6fbe95625e2fb:2

Which points to this line of the minified file:

Screen Shot 2021-04-07 at 11 33 00 AM

Which I believe I narrowed down to being this line of code

That's as far as I got, hope it is helpful in debugging this.