Accessing SiteConfig from BuildTask

Silverstripe Version: 4.1

Question: Accessing site config in a task

I’m in the process of upgrading a task from another site that run v3.6, part of which uses an extended SiteConfig. In 3.6, the following would work:

SiteConfig::current_site_config();

However, this doesn’t work in my 4.1 version. I admit - it’s highly likely to be me as I’m just getting to grips with v4, but even following a guide from the SS website (which shows the same as above), doesn’t work.

As I said, likely I’m missing something obvious (especially on a Monday morning!), but any help appreciated!

Can you expand on “doesn’t work”?

Does it throw any errors?

I am so sorry. I can’t believe I even posted that :roll_eyes:. It’s one of my bugbears too…

The error I see is:

Error: Class 'SilverStripe\Site\SiteConfig' not found

Which is why I believe it’s more of a me issue. I’ve looked through some of the docs, and found plenty of examples of using them in templates (which I’ve done successfully), but very few examples of using them in a PHP class, like a BuildTask. This is one example that I found, but it’s very very basic. The tutorial doesn’t seem to touch on this either, only the template side of things (unless I’m missing something - which is highly possible!)

Are you importing the class at the top of your task file?

use SilverStripe\SiteConfig\Config;

1 Like

^^ what he said,

but I think it should just be:

use SilverStripe\SiteConfig;

Thanks @Wheat and @Tim. I did actually try this, but for my own sanity, tried it again, and got:

[Emergency] Uncaught Error: Class 'SilverStripe\SiteConfig\Config' not found

Although, I had not tried just SilversStripe\SiteConfig, but this also returns:

[Emergency] Uncaught Error: Class 'SilverStripe\SiteConfig' not found

Just to confirm:

  • The task is in it’s own custom namespace (the one used throughout the site
  • It extends BuildTask
  • It includes
    • SilverStripe\Dev\BuildTask
    • SilverStripe\SiteConfig

Yeah, sorry… my fault. It should be:

use SilverStripe\SiteConfig\SiteConfig;

1 Like

So simple :slight_smile:

Thanks both - all working now (until I hit my next problem!)

2 Likes

The docs have now been updated to include the use statement :wink:

2 Likes