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

Call to undefined function Carbon_Fields\Provider\add_filter() with Bedrock / PHPUnit #899

Open
dimitriBouteille opened this issue Jun 24, 2020 · 10 comments

Comments

@dimitriBouteille
Copy link

dimitriBouteille commented Jun 24, 2020

Version

  • Carbon Fields: 3.1
  • WordPress: 5.4
  • PHP: 7.3

Expected Behavior

I just installed the carbon-field-icon package (https://packagist.org/packages/htmlburger/carbon-field-icon) and I have the following error:

Uncaught Error: Call to undefined function Carbon_Fields\Provider\add_filter() in ...\web\app\libs\carbon-fields\core\Provider\Container_Condition_Provider.php on line 254

I tried to create a custom field, but I have the same problem ...
I’m using Bedrock for the setup.

My composer.json

"extra": {
    "installer-paths": {
      "web/app/libs/{$name}/": ["htmlburger/carbon-fields", "htmlburger/carbon-field-icon"],
      "web/app/mu-plugins/{$name}/": ["type:wordpress-muplugin"],
      "web/app/plugins/{$name}/": [ "type:wordpress-plugin" ],
      "web/app/themes/{$name}/": ["type:wordpress-theme"]
    }
  },

Fix

#899 (comment) Careful, this is a temporary solution.
https://discourse.roots.io/t/global-autoload/18863/2

@acasto
Copy link

acasto commented Jul 25, 2020

I'm seeing the same issue with carbon-field-number when running phpunit. It seems to stem from field.php file being autoloaded which automatically runs some code as part of extend() that tries to use add_filter() whether or not WordPress is actually loaded at the time. I'm not sure what the proper fix would be but in the meantime I just added a test to check in the register() method of core/Provider/Container_Condition_Provider.php:

                if ( defined( 'ABSPATH' ) ) {
                        $this->install_container_conditions( $ioc );
                }

@mihdan
Copy link

mihdan commented Jul 31, 2020

Are there any changes on this issue?

@piggydoughnut
Copy link

Experiencing the same issue when running phpunit after installing it through composer.

Carbon Fields: 3.1
WordPress: 5.4.2
PHP: 7.3.11
Fatal error: Uncaught Error: Call to undefined function Carbon_Fields\Provider\add_filter() in /Users/dariah/Documents/Dev/sample_wordress/wp-content/plugins/wp-pp-plugin/vendor/htmlburger/carbon-fields/core/Provider/Container_Condition_Provider.php:254
Stack trace:
#0 /Users/dariah/Documents/Dev/sample_wordress/wp-content/plugins/wp-pp-plugin/vendor/htmlburger/carbon-fields/core/Provider/Container_Condition_Provider.php(21): Carbon_Fields\Provider\Container_Condition_Provider->install_container_conditions(Object(Carbon_Fields\Pimple\Container))
#1 /Users/dariah/Documents/Dev/sample_wordress/wp-content/plugins/wp-pp-plugin/vendor/htmlburger/carbon-fields/core/Pimple/Container.php(274): Carbon_Fields\Provider\Container_Condition_Provider->register(Object(Carbon_Fields\Pimple\Container))
#2 /Users/dariah/Documents/Dev/sample_wordress/wp-content/plugins/wp-pp-plugin/vendor/htmlburger/carbon-fields/core/Carbon_Fields.php(385): Carbon_Fields\Pimple\Container->register(Object(Carbon_Fields\Provider\Container_Condition_Provider) in /Users/dariah/Documents/Dev/sample_wordress/wp-content/plugins/wp-pp-plugin/vendor/htmlburger/carbon-fields/core/Provider/Container_Condition_Provider.php on line 254

@dimitriBouteille
Copy link
Author

Hi @piggydoughnut ,

Are you using Bedrock ?

@piggydoughnut
Copy link

piggydoughnut commented Aug 4, 2020

Hey @dimitriBouteille!

Ah, actually not. I missed that in your question. I just did a simple setup using composer https://phpunit.de/

@dimitriBouteille dimitriBouteille changed the title Call to undefined function Carbon_Fields\Provider\add_filter() with Bedrock Call to undefined function Carbon_Fields\Provider\add_filter() with Bedrock / PHPUnit Aug 8, 2020
@hafrang
Copy link

hafrang commented Aug 8, 2020

Hello Dimitri,

I had the same error while trying to create a new custom field into a plugin (i.e. into a theme).

I think that Bedrock try to load the custom field before WordPress is loaded, that's the reason why you get this error.

I handled it by removing the autoload of my custom field anywhere, then I generated the autoload with composer dump-autoload. Finally I required the autoload.php file from my plugin initialisation and it works.

Please, note that, this a temporary solution, you have to fix the autoload before sending it to a production environment.

@dimitriBouteille
Copy link
Author

Hi @piggydoughnut and @mihdan

@hafrang found a solution to fix the bug. Careful, as Hafrand says, this is a temporary solution.

Do not hesitate to test this fix and make a return :)

@peterkracik
Copy link

@dimitriBouteille I don't think this should be closed, it isn't resolved yet, it is only a hack

@dimitriBouteille
Copy link
Author

Hello @peterkracik , I’m reopening the issue but I’m not sure he has a new answer :(

Do not hesitate to propose a solution to fix this problem :)

@dimitriBouteille
Copy link
Author

@jorostoyanov Do you have any idea how to fix this problem?

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

6 participants