Fatal PHP error when onboarding an existing site with sage theme and bedrock. Class 'Fetchers/Other/CustomFieldFetcher' not found.'

Hiya,

I’m trying to onboard a pre-existing site which uses Sage theme (Version: 8.4.2) and Bedrock and I’ve managed to set it up in Local so that the site is functioning, and I can access wp-admin. However when I activate the Sage theme I get this PHP Error on the front end: Fatal error: Uncaught Error: Class ‘Fetchers\Other\CustomFieldFetcher’ not found in /app/bedrock/web/app/themes/ag/lib/helpers.php on line 38

If I look in the theme in helpers.php there’s this code at line 38:

function custom_field($field = '', $post_id = '')
{
    return (new \Fetchers\Other\CustomFieldFetcher)->fetchOne($field, $post_id);
}

And in lib there is the following directory structure and CustomFieldFetcher file (lib/Fetchers/Other/CustomFieldFetcher.php)

Strangely this theme works fine on the server I’ve pulled it down from, but it fails on my local setup and I can’t for the life of me figure out why.

Some details from Local of my local dev environment:

23

Any help with pointing me in the right direction would be greatly appreciated!

An update:

It seem that this is an issue with any Namespaced class that is being called from another file. Any namespaced class that’s referenced from helpers.php is failing in the same way.

Have you ran composer install in the theme directory?

There wasn’t a composer.json in the theme directory.

However I figured maybe there should be so I ran this command elsewhere (composer create-project roots/sage adamsage 8.4.2) and copied the resulting composer.json from the theme that was generated into my theme and ran composer install, it installed something but it doesn’t seem to have affected the error at all.

I don’t know if this will help but here is what the file structure looks like for the theme with lib and vendor expanded:

It looks like the project you copied is missing the package Fetchers\Other\CustomFieldFetcher.
You’ll need to review the codebase you copied from and find what is missing.

Also, rather than creating a new composer.json using composer create-project roots/sage adamsage 8.4.2, you should copy from the pre-existing project.

Thanks for replying. The thing that’s confusing is that the theme does appear to have that file:

41

And on the server this theme with these exact files is running fine with no fatal errors so it appears to me that what I have includes all the dependencies.

In Sage projects, Composer’s autoloader is generally used to autoload namespaced functions, etc. If packages are installed 2ith composer, they it automatically adds them to it’s autoloader. It looks like your Fetchers are outside of the vendor folder, so the original developer probably configured rules for autoloading them in the there’s composer.json so that Composer could generate correct autoloading rules. Composer docs explain this process: https://getcomposer.org/doc/01-basic-usage.md#autoloading

There should definitely have been a composer.json in the original theme, which would contain this information. If you can’t find it, and can’t get ahold of the original developer, you may need to rebuild it from scratch. The new (presumably empty) composer.json you just created probably overwrote the existing autoloader when you ran composer install.

Thanks everyone for your help! Helping me understand a bit more about autoloaders and Sage got me going in the right direction.

I have managed to get it working, in the end it turned out the config that was being used for dependencies was in the composer.json file a few levels up at the bedrock level, so I had everything that was needed already I was just screwing up the nginx config I think.

So I started again by pulling all the files down from the server and double checking the site’s nginx config pointed to the right place. Ran composer install from there (although I guess I didn’t need to really since the deps were there already) and everything within the theme seems to work properly now :slight_smile:

This topic was automatically closed after 42 days. New replies are no longer allowed.