Search code examples
drupaldrupal-modulesdrupal-theming

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


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 ..


Solution

  • 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.
    }