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

5

12

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

Esther answered 19/12, 2010 at 13:48 Comment(0)
E
38

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.
}
Enroot answered 4/1, 2013 at 11:9 Comment(1)
And you can use it like this: if (path_is_admin(current_path())) { // Do stuff. }Liebermann
C
7

I don't fully understand your end goal, but here are two answers to your question:

1) if (arg(0) == 'admin') { ... } will indicate if someone is in the admin section, since the entire admin section has paths prefixed with admin/

2) At admin/settings/admin/theme you can select a separate theme for the admin section, and then you'll know someone is in admin when that theme is loading rather than the main theme.

Corespondent answered 19/12, 2010 at 17:41 Comment(1)
thanks for the reply , the front end theme is displayed in the Block management section even though i have a separate theme selected for the admin section.i want to display the admin theme for every links in the admin section.Esther
Z
2

I'm not exactly sure what your ultimate goal is either. More explanation?

If you're checking for the path, Scott Reynen's #1 should do the trick.

Drupal 6 also has a default variable, $is_admin. This will be TRUE if the current user has admin access. Checking for this variable might be helpful. For more info, see here: http://api.drupal.org/api/drupal/modules--system--page.tpl.php/6

To your follow-up question, the front end theme is ALWAYS displayed in admin/build/block. This is correct and expected behavior, since while you're moving blocks around and assigning them to theme sections, you'd want to know where they're going in the theme you are configuring. If you have multiple themes enabled, try click on a different theme's settings (secondary row) while at admin/build/block. You'll see what I mean.

Zacek answered 23/12, 2010 at 23:38 Comment(0)
D
0

For the first question the above solutions must do....coming to the second one,it seems that you want to add the login block to the center (ie;the main content ) of the page and that too only on some pages..if this is the case you need to go to the blocks config page and set the "User Login" block to the desired place on the page and click save. Then again click configure next to the block and you will get the options to configure the settings like "on which pages this block must be shown"..etc..etc..

Divaricate answered 29/12, 2010 at 21:6 Comment(0)
C
0

For Drupal 6: @timmy and @Scott Reynen -- arg(0) and substr($_GET['q'], 0, 5) equals 'admin' will miss some patterns. The code below gets all the Admin page urls on my site, your site may have more or less.

$arg = arg(); 
$isAdminPage = ($arg[0] == 'admin' || $arg[2] == 'edit' || $arg[0] == 'user' || $arg[2] == 'workflow' || $arg[2] == 'statistics');  
if ($isAdminPage) { 
  // do admin stuff
}
Conlin answered 5/5, 2014 at 19:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.