htmlburger / carbon-fields

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

Conditional logic in Gutenberg block fields no longer working #800

Closed ChipRyan closed 3 years ago

ChipRyan commented 4 years ago

Version

Expected Behavior

Show/hide fields based on other field(s) in the same Gutenberg Block

Actual Behavior

All fields show by default, the conditional logic has no effect

Container definition

Block::make( __( 'My Shiny Gutenberg Block' ) )
    ->add_fields( array(
        Field::make( 'select', 'crb_show_socials', 'Show Socials' )
            ->add_options( array(
                'yes' => 'Yes',
                'no' => 'No',
            ) ),

        Field::make( 'text', 'crb_facebook', 'Facebook URL' )
            ->set_conditional_logic( array(
                'relation' => 'AND',
                array(
                    'field' => 'crb_show_socials',
                    'value' => 'no',
                    'compare' => '=',
                )
            ) ),
    ) )
    ->set_render_callback( function ( $fields, $attributes, $inner_blocks ) {
        ?>

        <?php
    } );

Comments

Tested on a fresh WP install. Console errors/warnings screenshot below. I reckon it has something to do with Gutenberg v6 in WP 5.3.

image

Eneva commented 4 years ago

We have the same issue

fabienlege commented 4 years ago

Same issue for me, any news ?

HeroBanana commented 4 years ago

Still an issue in version v3.1.11.

HeroBanana commented 4 years ago

After some debugging I found out that the this line cause issues: https://github.com/htmlburger/carbon-fields/blob/development/packages/core/hocs/with-conditional-logic/index.js#L55

The property effect for blocks contains an object of name -> value but for metaboxes it is the default one.

By using has function we can check the field:

// Possible fix for issue 800
// When function has has failed use find.
const fieldExists = has(effect, props.name) || find( effect, [ 'id', props.id ] );
ChipRyan commented 4 years ago

Still an issue in v3.1.14

HeroBanana commented 4 years ago

@jorostoyanov what is the status?

ernilambar commented 4 years ago

Any update on this?

peter-brennan commented 4 years ago

still an issue in v3.1.20. Eagerly waiting on a fix for this.

jcruz-qgiv commented 4 years ago

Any update on this?

zakariabinsaifullah commented 4 years ago

Conditional logic in Gutenberg block fields is not working at all, any update on it??

jorostoyanov commented 3 years ago

@HeroBanana

Your solution seems to fix this issue.

I have committed it to the development branch - ff6ac7808f17c04e4255cc267bb2185290db5800

It will be available shortly in the new release!

dimitriBouteille commented 3 years ago

Hi @jorostoyanov , I reopen this isssue because conditional logic working :( I have this code :

$extend[] = Field::make('select', 'action_type', "Type d'action")
    ->set_options([
        'link_action' => "Lien personnalisé",
        'custom_action' => "Action personnalisée"
    ]);
$extend[] = Field::make('select', 'action_name', 'Action')
    ->set_options([
        'hello' => 'Hello',
        'test' => 'Test',
        'votirue' => 'Voiture'
    ])
    ->set_conditional_logic([
        'relation' => 'AND', [
            'field' => 'action_type',
            'value' => 'custom_action',
            'compare' => '=',
        ]
    ]);
$extend[] = Field::make('text', 'button_label', "Nom du lien")
    ->set_conditional_logic([
        'relation' => 'AND', [
            'field' => 'action_type',
            'value' => 'custom_action',
            'compare' => '=',
        ]
    ]);
$extend[] = Field::make('text', 'button_link', "Lien")
    ->set_conditional_logic([
        'relation' => 'AND', [
            'field' => 'action_type',
            'value' => 'custom_action',
            'compare' => '=',
        ]
    ]);

The conditionnal logic work for action_name field, but not working for button_label and button_link fields.

Capture d’écran du 2021-02-14 18-32-22

Capture d’écran du 2021-02-14 18-31-53

I have a small error in the console :

Warning: WithEffects defines an invalid contextType. contextType should point to the Context object returned by React.createContext(). However, it is set to undefined. This can be caused by a typo or by mixing up named and default imports. This can also happen due to a circular dependency, so try moving the createContext() call to a separate file.
    in WithEffects
    in Unknown (created by WithSelect(WithEffects))
    in WithSelect(WithEffects) (created by WithConditionalLogic(WithDispatch(WithDispatch(WithEffects))))
    in WithConditionalLogic(WithDispatch(WithDispatch(WithEffects))) (created by WithFilters(WithFilters(SelectField)))
    in WithFilters(WithFilters(SelectField)) (created by BlockEdit)
    in div (created by Field)
    in div (created by Field)
    in Field
    in Unknown (created by WithFilters(Field))
    in WithFilters(Field)
    in Unknown (created by WithSelect(WithFilters(Field)))
    in WithSelect(WithFilters(Field)) (created by WithFilters(WithSelect(WithFilters(Field))))
    in WithFilters(WithSelect(WithFilters(Field))) (created by BlockEdit)
    in div (created by BlockEdit)
    in BlockEdit
    in Unknown (created by WithSelect(BlockEdit))
    in WithSelect(BlockEdit) (created by Edit)
    in Edit (created by WithToolbarControls(Edit))
    in WithToolbarControls(Edit) (created by WithInspectorControl(WithToolbarControls(Edit)))
    in WithInspectorControl(WithToolbarControls(Edit)) (created by WithInspectorControl(WithInspectorControl(WithToolbarControls(Edit))))
    in WithInspectorControl(WithInspectorControl(WithToolbarControls(Edit))) (created by WithToolbarControls(WithInspectorControl(WithInspectorControl(WithToolbarControls(Edit)))))
    in WithToolbarControls(WithInspectorControl(WithInspectorControl(WithToolbarControls(Edit))))
    in Unknown (created by WithDispatch(Component))
    in WithDispatch(Component)
    in Unknown (created by WithMultipleValidation(WithToolbarControls(WithInspectorControl(WithInspectorControl(WithToolbarControls(Edit))))))
    in WithMultipleValidation(WithToolbarControls(WithInspectorControl(WithInspectorControl(WithToolbarControls(Edit))))) (created by WithFilters(Edit))
    in WithFilters(Edit) (created by block_edit_BlockEdit)
    in block_edit_BlockEdit (created by block_BlockListBlock)
    in div (created by block_Block)
    in block_Block (created by block_BlockListBlock)
    in BlockCrashBoundary (created by block_BlockListBlock)
    in block_BlockListBlock (created by (block_BlockListBlock))
    in (block_BlockListBlock) (created by ((block_BlockListBlock)))
    in ((block_BlockListBlock)) (created by WithFontSizeInlineStyles(((block_BlockListBlock))))
    in WithFontSizeInlineStyles(((block_BlockListBlock)))
    in Unknown (created by WithSelect(WithFontSizeInlineStyles(((block_BlockListBlock)))))
    in WithSelect(WithFontSizeInlineStyles(((block_BlockListBlock)))) (created by WithDefaultAttributes(WithSelect(WithFontSizeInlineStyles(((block_BlockListBlock))))))
    in WithDefaultAttributes(WithSelect(WithFontSizeInlineStyles(((block_BlockListBlock))))) (created by WithFilters(block_BlockListBlock))
    in WithFilters(block_BlockListBlock) (created by IfCondition(WithFilters(block_BlockListBlock)))
    in IfCondition(WithFilters(block_BlockListBlock)) (created by WithDispatch(IfCondition(WithFilters(block_BlockListBlock))))
    in WithDispatch(IfCondition(WithFilters(block_BlockListBlock)))
    in Unknown (created by Pure(WithSelect(WithDispatch(IfCondition(WithFilters(block_BlockListBlock))))))
    in Pure(WithSelect(WithDispatch(IfCondition(WithFilters(block_BlockListBlock))))) (created by Items)
    in Items (created by BlockListItems)
    in BlockListItems (created by ForwardRef(BlockList))
    in div (created by ForwardRef(RootContainer))
    in div (created by InsertionPoint)
    in InsertionPoint (created by ForwardRef(RootContainer))
    in ForwardRef(RootContainer) (created by ForwardRef(BlockList))
    in ForwardRef(BlockList) (created by VisualEditor)
    in div (created by ObserveTyping)
    in ObserveTyping (created by WithSafeTimeout(ObserveTyping))
    in WithSafeTimeout(ObserveTyping) (created by VisualEditor)
    in div (created by WritingFlow)
    in div (created by WritingFlow)
    in WritingFlow (created by VisualEditor)
    in div (created by CopyHandler)
    in CopyHandler (created by VisualEditor)
    in div (created by Typewriter)
    in Typewriter
    in Unknown (created by WithSelect(Typewriter))
    in WithSelect(Typewriter) (created by VisualEditor)
    in div (created by BlockSelectionClearer)
    in BlockSelectionClearer (created by VisualEditor)
    in VisualEditor (created by Layout)
    in div (created by InterfaceSkeleton)
    in div (created by InterfaceSkeleton)
    in div (created by InterfaceSkeleton)
    in div (created by InterfaceSkeleton)
    in InterfaceSkeleton (created by NavigateRegions(InterfaceSkeleton))
    in div (created by NavigateRegions(InterfaceSkeleton))
    in NavigateRegions(InterfaceSkeleton) (created by Layout)
    in div (created by FocusReturnProvider)
    in FocusReturnProvider (created by Layout)
    in Layout (created by Editor)
    in ErrorBoundary (created by Editor)
    in BlockEditorProvider
    in Unknown (created by Context.Consumer)
    in WithRegistryProvider(BlockEditorProvider) (created by EditorProvider)
    in BlockContextProvider (created by EditorProvider)
    in EntityProvider (created by EditorProvider)
    in EntityProvider (created by EditorProvider)
    in EditorProvider (created by WithDispatch(EditorProvider))
    in WithDispatch(EditorProvider)
    in Unknown (created by WithSelect(WithDispatch(EditorProvider)))
    in WithSelect(WithDispatch(EditorProvider))
    in Unknown (created by Context.Consumer)
    in WithRegistryProvider(WithSelect(WithDispatch(EditorProvider))) (created by Editor)
    in div (created by DropZoneProvider)
    in DropZoneProvider (created by Editor)
    in slot_fill_provider_SlotFillProvider (created by SlotFillProvider)
    in SlotFillProvider (created by Editor)
    in StrictMode (created by Editor)
    in Editor (created by WithDispatch(Editor))
    in WithDispatch(Editor)
    in Unknown (created by WithSelect(WithDispatch(Editor)))
    in WithSelect(WithDispatch(Editor)) react-dom.js:82:32

Carbon field version : https://github.com/htmlburger/carbon-fields/releases/tag/v3.2.1 Wordpress: 5.6 PHP : 7.3

mrdarrengriffin commented 3 years ago

Same problem here, but I get no errors in the console. Works when using as a container for post_meta and theme_options but not a block:

image

Carbon Fields: 3.2.1 WordPress: 5.6.2 PHP: 7.3

klocus commented 3 years ago

The problem still exists (v3.4.0-beta.1). The logical conditions in Gutenberg don't work.

Tigriceo commented 2 years ago

Any update on this? What exactly is the problem that it cannot be solved?

stephleao commented 2 years ago

Same problem here. Logical conditions still don't work in Gutenberg. Did anybody find a solution?

Carbon Fields: 3.3.4 WordPress: 6.0.1 PHP: 7.4

AndryWJ commented 2 years ago

Also does not work in: carbon-fields: 3.3.4 wordpress: 6.0.2 php: 8.0.8

MattOndo commented 1 year ago

Still not working on the latest of CF, WP, and PHP :(

StudioRATATA commented 1 year ago

Looks like a broken record, but also not working in: Carbon Fields: 3.6.0 WordPress: 6.2 PHP: 8.0.8

my console throws An unknown field is used in condition - "field_name"

pshechko commented 7 months ago

Looks like a broken record, but also not working in: Carbon Fields: 3.6.0 WordPress: 6.2 PHP: 8.0.8

my console throws An unknown field is used in condition - "field_name"

@StudioRATATA did you manage to solve the issue?

mrdarrengriffin commented 5 months ago

This is still a problem for me many years later. I will look for the bug and make a PR

mrdarrengriffin commented 5 months ago

I think the conversation on this thread compared to the original issue might be highlighting 2 seperate issues.

For anyone having issues with conditional logic in that it says there is an unknown field, ensure that any nested fields can't be matched to the start of any parenting field name.

More info here: https://github.com/htmlburger/carbon-fields/issues/1064#issuecomment-2079976728

mrdarrengriffin commented 5 months ago

If anyone wants to take my findings so far and try and solve the problem, that would be amazing!