Saturday 21 May 2016

How to check if current path is admin path in drupal 8 ?- path_is_admin druapl 8

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

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