Example use case in druapl 7
Elijah Lynn commented
if (path_is_admin(current_path())) {
// Do stuff.
}
Drupal 8
To check the path of the current page:
$is_admin = \Drupal::service('router.admin_context')->isAdminRoute();
to check an arbitrary path, you need to create a Request object and obtain its matching Route:use Symfony\Component\HttpFoundation\Request;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
$path = '/node/1';
$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);
Drupal 7:$is_admin = path_is_admin($path);
Drupal 8:$is_admin = \Drupal::service('router.admin_context')->isAdminRoute(\Symfony\Component\Routing\Route $route);
// In order to get the $route you probably should use the $route_match
$route = \Drupal::routeMatch()->getRouteObject();
$is_admin = \Drupal::service('router.admin_context')->isAdminRoute($route);
* Omit the $path (Drupal 7) or $route (Drupal 8) parameter to check if the current page is an admin page.
Source : https://api.drupal.org/api/drupal/includes!path.inc/function/path_is_admin/7.x
No comments:
Post a Comment