How can I tell if request is made from within CMS?

Silverstripe Version: 4.11.

Question: How can I tell if the request is being made from within the CMS? Is there any function that I can use at DataObject level?

I am trying to find out if request is made from within CMS so that I can avoid parsing shortcodes within CMS context.

Cheers
Raghu

There’s not really any way to know what context a request is coming from without manually adding that context e.g. via query parameters.

You can to some extent identify what context the request is going to though - so if your in-CMS request is going through a ModelAdmin or other controller that descends from LeftAndMain then you can check if (Controller::has_curr() && (Controller::curr() instanceof LeftAndMain)).
You could alternatively check the request URL to see if it is in an admin-like route, which is how tractorcow/silverstripe-fluent handles it.

If you’re trying to tell whether your record is being viewed in the CMS preview panel or not, that’s even easier - just check for $request->getVar('CMSPreview') - if that’s truthy, then it’s in the CMS preview panel, otherwise it isn’t.

Ultimately it all depends on exactly what you mean by “if the request is being made from within the CMS” and what you’re trying to do with that information.

thanks Guy, that is what what I have implemented at the mo. I was checking if there is a better way or some un-documented function I can use to identify the context.

Cheers
R

I was checking if there is a better way or some un-documented function I can use to identify the context.

Makes sense. The problem with a method like that is depending on what you’re trying to do, the definition of “backend” or even “within the CMS” can be different. For example, is the CMS preview within the CMS? For the fluent module, it used to be (because there was a problem detecting the correct locale if it wasn’t considered “within the CMS”) - but it isn’t anymore (or won’t be once that change is merged in) because that limitation was resolved.