how to check if the user is in the admin part of drupal?

Sreejith Sasidharan picture Sreejith Sasidharan · Dec 19, 2010 · Viewed 12.8k times · Source

How do i check if the current page is in the admin section of drupal?. I want to display a login form in some pages from the main menu but the login page is displayed in the block selection menu .Please suggest a solution ..

Answer

gagarine picture gagarine · Jan 4, 2013

For Drupal 7 you can use path_is_admin() .

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

For Drupal 8 isAdminRoute()

$is_admin = \Drupal::service('router.admin_context')->isAdminRoute();
if ($is_admin) {
  // Do stuff.
}