Saturday 21 May 2016

How to check if current page is frontpage in drupal 8 - drupal_is_front_page drupal 8

How to check if current page is frontpage in drupal 8 

During the Drupal 8 development cycle a path.matcher was introduced which deprecated most procedural functions in path.inc
Drupal 7:
$path_matches = drupal_match_path($path, $patterns);
$is_front = drupal_is_front_page();
Drupal 8:
$path_matches = \Drupal::service('path.matcher')->matchPath($path, $patterns) ;
$is_front = \Drupal::service('path.matcher')->isFrontPage();
 
 
reff links: 
 
https://www.drupal.org/node/2274675 
 
Deprecated by path.matcher service.
https://www.drupal.org/node/2274675

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