carbon-fields icon indicating copy to clipboard operation
carbon-fields copied to clipboard

Gutenberg: set_style throws "Style is not enqueued"

Open mrksdiehl opened this issue 4 years ago • 3 comments

Version

  • Carbon Fields: 3.1
  • WordPress: 5.2.2
  • PHP: 7.2

Expected Behavior

I add function in the carbon_fields_register_fields hook. First thing I register my style accordion-style with wp_register_style. In the Block, after the fields I set ->set_style('accordion-style'). I expect the style being loaded.

Actual Behavior

Carbon fields throws an "Style accordion-style is not enqueued." exception. This is thrown in core/Block_Container.php on line 192.

Container definition

        wp_register_style(
                'accordion-style',
                plugins_url( "styles/accordion-style.css", __FILE__ )
        );
        Block::make( 'Accordion' )
        ->add_fields(
            array(
                Field::make('complex', 'items')->add_fields(
                    array(
                        Field::make( 'text', 'title' ),
                        Field::make( 'rich_text', 'content' )
                    )
                )->set_collapsed( true )
            )
        )
        ->set_description( 'Simple accordion' )
        ->set_style( 'accordion-style' )
        ->set_render_callback( array( self::class, 'render' ) );

Steps to Reproduce the Problem

  1. Register style
  2. Add style to block

Comments

If I remove the wp_style_is check in Block_Container.php, the block loads correctly and the style is actually enqueued and loaded as well. If I check global $wp_styles, the the style is in there. No matter what though, wp_style_is will always return false.

mrksdiehl avatar Jul 23 '19 08:07 mrksdiehl

After checking the documentation of wp_style_is it shows that this function by default only checks for enqueued styles. By passing registered as a second argument, it actually checks the registered styles and it works. So one could change the function to

if ( ! wp_style_is( $handle, 'registered' ) && ! wp_style_is( $handle) ) {
	throw new \Exception( __( "Style '$handle' is not enqueued.", 'crb' ) );
}

mrksdiehl avatar Jul 24 '19 10:07 mrksdiehl

Faced the same problem

vanilium avatar Dec 03 '19 12:12 vanilium

I believe this pull request fix #1001 this.

labmorales avatar May 04 '21 11:05 labmorales

This issue was fixed in the current release (v3.5.0). Please update the WordPress core and Carbon Fields to the newest version and let us know if the issues still exist.

HTMLBurger-NG avatar Feb 20 '23 09:02 HTMLBurger-NG