9

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.

  • 1
    Node edit is not an admin page. – ya.teck Nov 3 '16 at 7:33
  • 1
    You can make it an admin path by toggling the "Use the administration theme when editing or creating content" option under "Appearance". That makes all routes with the option _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. – TwoD Mar 27 '17 at 12:23
13

* 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')->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()->getRouteObject();

  $is_admin = \Drupal::service('router.admin_context')->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')->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()->getRouteObject();

  $is_admin = FALSE;
  if (!empty($route)) {
    $is_admin_route = \Drupal::service('router.admin_context')->isAdminRoute($route);
    $has_node_operation_option = $route->getOption('_node_operation_route');
    $is_admin = ($is_admin_route || $has_node_operation_option);
  }
  else {
    $current_path = \Drupal::service('path.current')->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;
    }
  }
  • 2
    Using RegEx like that is indeed bad practice and should be avoided. Until a solution is found you should check if the route name is 'entity.node.edit_form' – Eyal Nov 1 '16 at 23:32
  • the problem is $route is returning null, empty. I wouldn't need to do the regex if ->getRouteObject() returned the route. – oknate Nov 2 '16 at 3:45
  • Weird. Have you tried \Drupal::routeMatch()->getRouteName() – Eyal Nov 2 '16 at 6:55
  • 1
    \Drupal::routeMatch()->getRouteName() gives me null (Drupal 8.3.2) – Guillaume Bois May 10 '17 at 13:36
  • 1
    Your additional check is missing admin pages like node revisions, delete and translations. – Philipp Michael Jul 5 '17 at 10:03
8

The following is a bit more concise, and also captures node edit pages when they're configured to use the admin theme. The isAdminRoute method uses the current Route if none is specified:

  /** @var \Drupal\Core\Routing\AdminContext $admin_context */
  $admin_context = \Drupal::service('router.admin_context');
  if (!$admin_context->isAdminRoute()) {
    // perform tasks.
  }
  • "captures node edit pages when they're configured to use the admin theme" is key here. The reason I asked the question, is because I was having trouble capturing non-admin-theme pages such as node edit pages when they are not using the admin theme. – oknate May 16 '17 at 15:48
  • Ah, that makes sense. Yes, it's more complicated when the node edit pages are not set as admin pages. – Shaun Dychko May 17 '17 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.