Drupal how to determine if an admin theme is being used for a given path
So this was a strange one. Normally you can determine a lot of information by just accessing the current theme or route of a given request a user is making. However, in my instance you couldn't access that since I was using Behat, and it's Drupal session was separate from the given users page load. Thus, I only had the current path available to me (IE: node/1234/edit) and not much else to go off of. I had to use that information to determine if the current path was loading the admin theme or not. Here's what I was able to make work:
$current_path = you_get_this_however_you_can();
$router = \Drupal::service('router.no_access_checks');
$route_values = $router->match($current_path);
$route = $route_values['_route_object'];
if (!$route->getOption('_admin_route')) {
throw new \Exception('The current page is not loaded using the admin theme.');
}
So the above works, but please note one pitfall: It does not check if the user has actual access to that page. So if you request a page, the users gets a 403 or 404 response and an un-themed 403/404 page, the above will not throw the exception as the page if rendered with proper access is an admin route.
In Behat, I placed the following above this code to check for non 200 responses:
// Check status code since we are checking routes without access considerations.
$status_code = $this->getSession()->getStatusCode();
if ($status_code > 299) {
throw new \Exception(sprintf('The current page returned a non 2** status code of '));
}
Hopefully any of this helps you in your efforts to determine if a given path renders with the admin theme or not!