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

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

Open dimitriBouteille opened this issue 5 years ago • 10 comments

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

https://github.com/htmlburger/carbon-fields/issues/899#issuecomment-670926826 Careful, this is a temporary solution. https://discourse.roots.io/t/global-autoload/18863/2

dimitriBouteille avatar Jun 24 '20 12:06 dimitriBouteille

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 );
                }

acasto avatar Jul 25 '20 14:07 acasto

Are there any changes on this issue?

mihdan avatar Jul 31 '20 10:07 mihdan

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

piggydoughnut avatar Aug 02 '20 23:08 piggydoughnut

Hi @piggydoughnut ,

Are you using Bedrock ?

dimitriBouteille avatar Aug 03 '20 08:08 dimitriBouteille

Hey @dimitriBouteille!

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

piggydoughnut avatar Aug 04 '20 05:08 piggydoughnut

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.

hafrang avatar Aug 08 '20 13:08 hafrang

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

dimitriBouteille avatar Aug 08 '20 13:08 dimitriBouteille

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

peterkracik avatar Sep 21 '21 13:09 peterkracik

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 avatar Sep 23 '21 23:09 dimitriBouteille

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

dimitriBouteille avatar Sep 23 '21 23:09 dimitriBouteille