How to test if current page is an admin page? At some point I started using the following code to test if current page is an admin page:

  $route = \Drupal::routeMatch()-&gt;getRouteObject();

  $is_admin = \Drupal::service('router.admin_context')-&gt;isAdminRoute($route);


But I found that this fails on node edit pages in hook_entity_load().

How do we test if we are on an admin page in Drupal 8?  Something like path_is_admin() in Drupal 7?

I found that if I test the same node edit page path from outside hook_entity_load, using the code on this page (https://api.drupal.org/api/drupal/includes%21path.inc/function/path_is_admin/7.x) it works.

use Symfony\Component\HttpFoundation\Request;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;

$path = '/node/75/edit';
$request = Request::create($path);

$route_match = \Drupal::service('router.no_access_checks')-&gt;matchRequest($request);
$route = $route_match[RouteObjectInterface::ROUTE_OBJECT];
$is_admin = \Drupal::service('router.admin_context')-&gt;isAdminRoute($route);


But if I try to add this code in hook_entity_load using the current path, I get a  fatal loop.

I think where I'm stuck is loading the current route in hook_entity_load.
* Updated *

When I first answered this question, node edit and taxonomy edit pages weren't set as admin routes.  So I added separate checks for those.  Such separate checks aren't necessary any more.  The following seems to cover it pretty well:

if (\Drupal::service('router.admin_context')-&gt;isAdminRoute()) {
 // do stuff
}


If you want to check a route other than the current route, you can pass it into isAdminRoute().

Note, the above way won't work for node edit pages or taxonomy term edit pages if you uncheck "Use the administration theme when editing or creating content" on the theme admin page at /admin/appearance.  Then you'd need a separate check.

* Original Answer: *

To test if a page is an admin page, you can use a two step process.  Since regex for the node edit may not be using the admin theme.

First I use the isAdminRoute method:

  $route = \Drupal::routeMatch()-&gt;getRouteObject();

  $is_admin = \Drupal::service('router.admin_context')-&gt;isAdminRoute($route);


Then you can use an additional check for the path to see if it's a node edit page:

  $current_path = \Drupal::service('path.current')-&gt;getPath();
  if(preg_match('/node\/(\d+)\/edit/', $current_path, $matches)) {
    $is_admin = TRUE;
  }


Here's a pretty elaborate, but pretty complete way to test for node and taxonomy term edit pages as well as other admin routes:

  $route = \Drupal::routeMatch()-&gt;getRouteObject();

  $is_admin = FALSE;
  if (!empty($route)) {
    $is_admin_route = \Drupal::service('router.admin_context')-&gt;isAdminRoute($route);
    $has_node_operation_option = $route-&gt;getOption('_node_operation_route');
    $is_admin = ($is_admin_route || $has_node_operation_option);
  }
  else {
    $current_path = \Drupal::service('path.current')-&gt;getPath();
    if(preg_match('/node\/(\d+)\/edit/', $current_path, $matches)) {
      $is_admin = TRUE;
    }
    elseif(preg_match('/taxonomy\/term\/(\d+)\/edit/', $current_path, $matches)) {
      $is_admin = TRUE;
    }
  }