Drupal - 如何检查是否在管理页面上?

Drupal - How to check if on admin page?

我在 Drupal 7 安装的 page.tpl.php 中使用了以下代码:

if (!path_is_admin(current_path())) {
    $pathArray = explode('/', current_path());
    if (!empty($pathArray)) {
        $path_to_node = url("node/".$pathArray[1]);
        $menuChildArray = explode('/', $path_to_node);
        $menuParent = $menuChildArray[2];
    }
}

但是在管理界面的某些页面上,我得到:

Notice: Undefined offset: 2 in include() (line 36 of /home/www/doc/7622/s-d-d.de/testkc/sites/all/themes/sdd2015/page.tpl.php).

只有在前端时才应该执行代码...?为什么它在后端被触发?

猜测:

该消息实际上是在您访问前端页面时生成的,但是由于您在page.tpl.php中有此代码(不应该在的地方),因此无法在消息区域直到 下一个 页面视图,因为当前页面的消息已经打印在同一个文件中。

因此您的代码可能按预期工作,但您发现通知消息的输出出现延迟。

一如既往,解决方案是在使用之前检查您的变量。

// Or whatever conditional makes sense to what you're trying to do.
if (!empty($menuChildArray[2])) {
  $menuParent = $menuChildArray[2];
}