Roots Discourse

Trellis + Bedrock + Carbon Fields + Composer custom install location = autoloader exception

Carbon Fields when required by Composer needs to live in the public WP directory. You can fix that easily enough here: [https://carbonfields.net/docs/guides-bedrock-support/](CF + Bedrock guide). When deploying with Trellis I started getting autoloader exceptions for Carbon Fields from the wp-cli during the deploy.

Finally figured out that Carbon Fields was not making it into the Bedrock web/wp directory on the first pass of composer install. I went into the site install and manually ran composer install and boom it all works since there is a web/wp to stick CF into when composer install runs the second time . I edited the after-build hook that calls composer install to simply call composer install twice in a row. I got it working, yet is there a better way to solve this issue?

Why are you trying to put it in web/wp? The guide doesn’t mention anything about that location, and generally nothing should go there. The public directory for Bedrock is just web, so I feel like you should be able to just do

"extra": {
    "installer-paths": {
        "web/vendor/{$vendor}/{$name}": ["htmlburger/carbon-fields"]
    }
}

Hi ! I have a problem with carbon fields too, please, can someone look at it, it’s been 3 days I try to clear this… I will remove this reply if totally out of scope.

When I set CF to install into web/vendor, autoload works fine yet CF tries to serve its assets for displaying the actual fields from the wp directory instead of the web/vendor directory. (The CF container shows up, just not the actual fields for editing. I also get a whole slew of 404s in the developer console for the CF css/js)

When CF installs into web/wp, everything works as expected on the CF side. I just have to use my silly hack of two composer calls in a row to get CF into where it wants to be.

For now, I’ll go with the double composer calls. This is a CF issue more than a trellis/bedrock issue.

1 Like

I haven’t tested this (I’m just on my phone) but it looks like this method is what determines that url: https://github.com/htmlburger/carbon-fields/blob/55df956eb41b016516c1bf3a537348c752416241/core/Carbon_Fields.php#L228

…But you can define it yourself as a constant and then it’ll use the constant instead: https://github.com/htmlburger/carbon-fields/blob/55df956eb41b016516c1bf3a537348c752416241/config.php#L15

So maybe you could set that constant to the correct url and bypass the logic that’s giving you a bad URL? It looks like the url logic also supports Carbon Fields being installed as a plug-in, so maybe you could for Composer to install it as a plug-in as a possible solution too.

1 Like

You sir are a wizard. You went above and beyond, which I certainly appreciate. Thank you for digging into the CF code. Defining the URL constant before booting CF did the trick. Below is the snippet that got me rolling again.

define('Carbon_Fields\URL', home_url('/vendor/htmlburger/carbon-fields'));
\Carbon_Fields\Carbon_Fields::boot();
2 Likes