At some point I started using the following code to test if current page is an admin page:
$route = \Drupal::routeMatch()->getRouteObject();
$is_admin = \Drupal::service('router.admin_context')->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')->matchRequest($request);
$route = $route_match[RouteObjectInterface::ROUTE_OBJECT];
$is_admin = \Drupal::service('router.admin_context')->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.
_node_operation_route
enabled also get the option_admin_route
.\Drupal::service('router.admin_context')->isAdminRoute()
checks that flag for the current route by default.