Carbon-fields: Class "Widget" is ironically not found

Created on 17 May 2018  路  8Comments  路  Source: htmlburger/carbon-fields

Version

  • Carbon Fields: ^2.2
  • WordPress: 4.9.5
  • PHP: 7.2.4

Expected Behavior

It should not display "Fatal error: Class 'Carbon_Fields\Widget' not found in \path\" like Field and Container worked.

Actual Behavior

According this screenshot https://i.stack.imgur.com/awjfV.png after of copying the example.

Container definition

use Carbon_Fields\Widget;
use Carbon_Fields\Field;

class ThemeWidgetExample extends Widget {
    // Register widget function. Must have the same name as the class
    function __construct() {
        $this->setup( 'theme_widget_example', 'Theme Widget - Example', 'Displays a block with title/text', array(
            Field::make( 'text', 'title', 'Title' )->set_default_value( 'Hello World!') ,
            Field::make( 'textarea', 'content', 'Content' )->set_default_value( 'Lorem Ipsum dolor sit amet' )
        ) );
    }

    // Called when rendering the widget in the front-end
    function front_end( $args, $instance ) {
        echo $args['before_title'] . $instance['title'] . $args['after_title'];
        echo '<p>' . $instance['content'] . '</p>';
    }
}

function load_widgets() {
    register_widget( 'ThemeWidgetExample' );
}

Steps to Reproduce the Problem

  1. Open and enable Xampp
  2. Open Theme directory in Wordpress
  3. Execute 'composer require htmlburger/carbon-fields' in Terminal
  4. Create style.css, index.php and functions.php, then copy this code above and paste inside of functions.php.

Comments

I'm frustrated for failing to understand this problem 馃

Most helpful comment

I see how the docs are confusing, so this is definitely an issue on our side. We'll update the docs in the coming days.

All 8 comments

Hi @joannesalfa,

An interesting case you have there. :)
Thanks for pointing this out, because this is actually a documentation issue that should be addressed (https://github.com/htmlburger/carbon-fields-docs/issues/35).

I think that the problem you are facing is related to the autoloader. If you have the following code:

use Carbon_Fields\Container;
use Carbon_Fields\Field;
use Carbon_Fields\Widget;

add_action( 'after_setup_theme', 'crb_load' );
function crb_load() {
    require_once( 'carbon-fields/vendor/autoload.php' );
    \Carbon_Fields\Carbon_Fields::boot();
}

class ThemeWidgetExample extends Widget {
    ...
}

This will throw an error saying that the Carbon_Fields\Widget is not found, because the autoloader, which actually loads the class, is triggered at a later stage (on the after_setup_theme action).

One option is to move the require_once( 'carbon-fields/vendor/autoload.php' ) outside of the function (not really recommended), another is to extract the widgets in a separate file and include it after the Carbon_Fields::boot() call. For example:

add_action( 'after_setup_theme', 'crb_load' );
function crb_load() {
    require_once( 'carbon-fields/vendor/autoload.php' );
    \Carbon_Fields\Carbon_Fields::boot();

    include_once( 'includes/widgets.php' );
}

Hope this helps. Cheers!

I'm running into the same error, but for blocks.

Fatal error: Uncaught Error: Class 'Carbon_Fields\Block' not found in /Users/brianpurkiss/Documents/Sites/boilerplate/wp-content/themes/wp-theme-boilerplate/functions/carbon-fields/gutenberg-test.php:7 Stack trace: #0 /Users/brianpurkiss/Documents/Sites/boilerplate/wp-content/themes/wp-theme-boilerplate/functions.php(44): require() #1 /Users/brianpurkiss/Documents/Sites/boilerplate/wp-settings.php(443): include('/Users/brianpur...') #2 /Users/brianpurkiss/Documents/Sites/boilerplate/wp-config.php(94): require_once('/Users/brianpur...') #3 /Users/brianpurkiss/Documents/Sites/boilerplate/wp-load.php(37): require_once('/Users/brianpur...') #4 /Users/brianpurkiss/Documents/Sites/boilerplate/wp-blog-header.php(13): require_once('/Users/brianpur...') #5 /Users/brianpurkiss/Documents/Sites/boilerplate/index.php(17): require('/Users/brianpur...') #6 /Users/brianpurkiss/.composer/vendor/laravel/valet/server.php(133): require('/Users/brianpur...') #7 {main} thrown in /Users/brianpurkiss/Documents/Sites/boilerplate/wp-content/themes/wp-theme-boilerplate/functions/carbon-fields/gutenberg-test.php on line 7

I'm using the code snippets provided here: https://docs.carbonfields.net/#/containers/gutenberg-blocks

For the quickstart instructions, I followed the "without composer" instructions of that matters.

I tried the above crb_load() function and it didn't help even when I tried to modify it to load block.php instead of widgets.

autoloader.php references composer, so I'm wondering my not using composer is the problem.

Hi @brianpurkiss,

Can you give us more information about your setup and the version of Carbon Fields that you're using?

I'm using version 3.0.1 (same issue happened with 3.0.0) and downloaded it through the /zip/latest/ URL.

I've tried working with a few different code snippets from the Gutenberg Blocks documentation page, and none of them are working. Currently this is what I have:

`

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_description( __( 'A simple block consisting of a heading, an image and a text content.' ) )
->set_category( 'custom-category', __( 'Custom Category' ), 'smiley' )
->set_icon( 'heart' )
->set_keywords( [ __( 'block' ), __( 'image' ), __( 'content' ) ] )

->set_render_callback( function ( $block ) {
    ?>

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

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

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

    <?php
} );

`

I have successfully gotten this other snippet from the quick start working:

`use Carbon_Fields\Container;
use Carbon_Fields\Field;

add_action( 'carbon_fields_register_fields', 'crb_attach_theme_options' );
function crb_attach_theme_options() {
Container::make( 'theme_options', __( 'Theme Options' ) )
->add_fields( array(
Field::make( 'text', 'crb_text', 'Text Field' ),
) );
}`

So carbon fields is working, the Gutenberg Blocks portion simply isn't working.

When I include the gutenberg blocks portion, I get the above pasted error.

Bump on this @vvasilev-

@brianpurkiss I just tested your use case and things worked fine for me. It looks like you're defining your block too early.

Can you please confirm that your call to Block::make is performed after the carbon_fields_register_fields hook?

That did it.

I needed to add:

use Carbon_Fields\Container;
use Carbon_Fields\Field;

And wrap my "Block::make" inside an:

add_action( 'carbon_fields_register_fields'

I thought it was super weird the Block::make wasn't wrapped inside a function, but I was just blindly (or is stupidly a better descriptor?) following the instructions on the gutenberg blocks page.

My documentation suggestion would be to wrap the Block::make example on the code snippets inside a function so it works as a copy/paste for new users like me, or add a paragraph to that page reminding people to wrap it in a add_action( 'carbon_fields_register_fields'

But it's working now and I feel a little stupid for not wrapping the code in an add_action, but I thought maybe y'all just have a unique and weird system. So that gives me a slight excuse... right?

Thanks for the help!

I see how the docs are confusing, so this is definitely an issue on our side. We'll update the docs in the coming days.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ArekZw picture ArekZw  路  4Comments

jquimera picture jquimera  路  4Comments

MDSilviu picture MDSilviu  路  3Comments

proweb picture proweb  路  3Comments

Mick00 picture Mick00  路  3Comments