Carbon-fields: Class 'Carbon_Fields\Block' not found in

Created on 28 Jun 2019  Β·  6Comments  Β·  Source: htmlburger/carbon-fields

ΠŸΡ€ΠΈΠ²Π΅Ρ‚ всСм, насколько я понял, Ρ‚ΡƒΡ‚ ΠΈ ΠΏΠΎ-русски Ρ€Π°Π·Π³ΠΎΠ²Π°Ρ€ΠΈΠ²Π°ΡŽΡ‚? Π£ мСня Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ΠΏΡ€ΠΎΠ±Π»Π΅ΠΌΠ° ΠΏΡ€ΠΈ ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠΈ Π±Π»ΠΎΠΊΠΎΠ² 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' );
`

Most helpful comment

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)

All 6 comments

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.

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 ?

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.

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? :)

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)

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

Was this page helpful?
0 / 5 - 0 ratings