Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional logic in Gutenberg block fields no longer working #800

Closed
ChipRyan opened this issue Dec 11, 2019 · 23 comments
Closed

Conditional logic in Gutenberg block fields no longer working #800

ChipRyan opened this issue Dec 11, 2019 · 23 comments
Assignees

Comments

@ChipRyan
Copy link

ChipRyan commented Dec 11, 2019

Version

  • Carbon Fields: 3.1.8
  • WordPress: 5.3
  • PHP: 7.3

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

@ChipRyan ChipRyan changed the title Conditional Logic in Gutenberg block fields no longer working Conditional logic in Gutenberg block fields no longer working Dec 11, 2019
@Eneva
Copy link

Eneva commented Dec 11, 2019

We have the same issue

@jorostoyanov jorostoyanov self-assigned this Dec 16, 2019
@fabienlege
Copy link
Contributor

Same issue for me, any news ?

@HeroBanana
Copy link
Contributor

Still an issue in version v3.1.11.

@HeroBanana
Copy link
Contributor

HeroBanana commented Jan 16, 2020

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
Copy link
Author

Still an issue in v3.1.14

@HeroBanana
Copy link
Contributor

@jorostoyanov what is the status?

@ernilambar
Copy link

Any update on this?

@peter-brennan
Copy link

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

@jcruz-qgiv
Copy link

Any update on this?

@zakariabinsaifullah
Copy link

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

@jorostoyanov
Copy link
Contributor

@HeroBanana

Your solution seems to fix this issue.

I have committed it to the development branch - ff6ac78

It will be available shortly in the new release!

@dimitriBouteille
Copy link

dimitriBouteille commented Feb 14, 2021

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
Copy link

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
Copy link

klocus commented Aug 25, 2021

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

@Tigriceo
Copy link

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

@stephleao
Copy link

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
Copy link

AndryWJ commented Sep 11, 2022

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

@MattOndo
Copy link

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

@StudioRATATA
Copy link

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
Copy link

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
Copy link

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

@mrdarrengriffin
Copy link

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: #1064 (comment)

@mrdarrengriffin
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests