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

Class 'Carbon_Fields\Block' not found in

Open Hellion35 opened this issue 7 years ago • 6 comments

Привет всем, насколько я понял, тут и по-русски разговаривают? У меня возникла проблема при подключении блоков Gutenberg. Беру пример из документации и вставляю его как есть. ` use Carbon_Fields\Block;

Block::make( __( 'My Shiny Gutenberg Block' ) ) ->add_fields( array( Field::make( 'text', 'heading', __( 'Block Heading' ) ), Field::make( 'image', 'image', __( 'Block Image' ) ), Field::make( 'rich_text', 'content', __( 'Block Content' ) ), ) ) ->set_render_callback( function ( $fields, $attributes, $inner_blocks ) { ?>

    <div class="block">
        <div class="block__heading">
            <h1><?php echo esc_html( $fields['heading'] ); ?></h1>
        </div><!-- /.block__heading -->

        <div class="block__image">
            <?php echo wp_get_attachment_image( $fields['image'], 'full' ); ?>
        </div><!-- /.block__image -->

        <div class="block__content">
            <?php echo apply_filters( 'the_content', $fields['content'] ); ?>
        </div><!-- /.block__content -->
    </div><!-- /.block -->

    <?php
} );

В итоге у меня выдает ошибку: Fatal error: Uncaught Error: Class 'Carbon_Fields\Block' not found in. Что я делаю не так? Подключение CarbonFields: function crb_load() { require_once( 'inc/vendor/autoload.php' ); \Carbon_Fields\Carbon_Fields::boot(); } add_action( 'after_setup_theme', 'crb_load' ); `

Hellion35 avatar Jun 28 '19 09:06 Hellion35

Hi @Hellion35

Could you please make sure that you're using Carbon Fields version 3? I guess that you're using older version of the library.

emohamed avatar Jun 28 '19 11:06 emohamed

I have same issue. I am using this version - https://carbonfields.net/zip/v3.1.2/ . It is a bug or file missing, but other things (theme option, metabox, widget etc) work fine just have issue with guttenberg block. Can you please recheck @emohamed ?

webangon avatar Jun 30 '19 14:06 webangon

I know this will be very obvious for most, but for me I had failed to switch to the theme where I had installed Carbon Fields. Embarrassingly this took me 10 - 15 minutes to realize.

james0r avatar Jun 30 '19 18:06 james0r

Hi @Hellion35,

You should register your blocks after the boot method call.

function crb_load() {
    require_once( 'inc/vendor/autoload.php' );
    \Carbon_Fields\Carbon_Fields::boot();

    // Register your blocks here...
}
add_action( 'after_setup_theme', 'crb_load' );

@webangon, Can you try the same thing? :)

vvasilev- avatar Jul 01 '19 14:07 vvasilev-

In terms of developer-friendliness, could this boot process be done automatically by the plugin under the hood, so that instructions get simpler and more consistent, like: Define your blocks within the action_X hook or any action hook between X and Z (X and Z being part of https://codex.wordpress.org/Plugin_API/Action_Reference)

drzraf avatar Jan 01 '20 17:01 drzraf

I been using 3.1.2 version, (Plugin Edition).

Register the component under carbon_fields_register_fields hooks works fine for me. add_action( 'carbon_fields_register_fields', 'custom_function_to_register_block' );

Full sample code: github gist

coder618 avatar Jun 18 '20 07:06 coder618