Verify, Define and Get Drupal admin paths

Last updated on 20/02/2016

Drupal 8

In Drupal8 an administrative path is defined in *.routing.yml file.

In order to mark a path as administrative, you'll have to add specific key-value pair into that yaml route definition:

  options:
    _admin_route: FALSE

Verify if the current page route is admin page:

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

Verify if a specific page route is admin page:

use Symfony\Component\HttpFoundation\Request;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
 
$request = Request::create('/node/1');
 
$route_match = \Drupal::service('router.no_access_checks')->matchRequest($request);
$route = $route_match[RouteObjectInterface::ROUTE_OBJECT];
if (\Drupal::service('router.admin_context')->isAdminRoute($route)) {
  // Do something...
}

*There is no any method which will retrieve all defined admin paths.

Drupal 7

By default a Drupal path is considered to be non-administrative. In order to mark a path as administrative here is the snippet:

/**
 * Implements hook_admin_paths().
 */
function module_name_admin_paths() {
  $paths = array(
    'node/*/my_custom_page' => TRUE,
  );
  return $paths;
}

Verify if current page is admin page:

if (path_is_admin(current_path())) {
  // Do something...
}

To retrive all defined admin paths use:

$admin_paths = path_get_admin_paths();