Philip Birk-Jensen
BirkAndMe

Follow

BirkAndMe

Follow

Drupal: Check if the current page is using the admin theme

Philip Birk-Jensen's photo
Philip Birk-Jensen
·Jun 2, 2022·

1 min read

Snippet

if (\Drupal::service('router.admin_context')->isAdminRoute()) {
  // ...
}

The router.admin_context service maps to the AdminContext helper class.

isAdminRoute() simply checks the Route for the _admin_route option.

Pitfalls

When working with a Route object, it's tempting to check for the _admin_route option directly, instead of using the router_admin_context service. The problem here is any customization of the service will be ignored.

 
Share this