18

How do I detect whether the user the page is currently viewing, is considered administrative? An administrative page can be recognized when the administration theme is activated.

In my case I would like to print a notice if this condition is met, and if the user can 'administer modules'.

30

This can be done with path_is_admin(current_path()).

For example, the following hook implementation can be used.

function mymodule_init() {
  if (user_access('administer modules') && path_is_admin(current_path())) {
      drupal_set_message(t('Message'));
  }
}
  • 1
    The answer depends on what the OP's definition of "administrative" is, and this definition may have changed since the question was first posted. Some consider content editing pages to be "admin" pages, yet path_is_admin() will return false for a page like node/123/edit. Meanwhile, this page may or may not use the admin theme. Any page might potentially use the admin theme, and any page might potentially not use the admin theme. The current definition in the OP is "An administrative page can be recognized when the administration theme is activated." – arnoldbird Aug 24 '13 at 15:28
9

Daniel's answer is correct for determining if the current path is "administrative," but if you want to more directly check if the administration theme is being used (your question seems a bit vague as to which you're looking for), you can do…

global $theme;
if ($theme === variable_get('admin_theme', 'seven')) {
  // …
}

…though this is likely to fail if you try to run it too early in the bootstrap process, namely before $theme is defined.

  • Clarified OP so it is not directed at looking for admin theme. Rather admin theme is simply an indication of an administrative path. – Daniel Oct 3 '12 at 11:41
  • I was still confused, until I saw that this is one of those things where you posted a question and then answered it yourself. Derp, I get it now. – Garrett Albright Oct 3 '12 at 13:30
2

For those looking for the Drupal 8 method:

if (\Drupal::service('router.admin_context')->isAdminRoute()) {
  // …
}

(Tested with a preprocess function in 8.7.5 and used in a core-patch in 8.0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.