简体   繁体   English

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

[英]Drupal - How to check if on admin page?

I use the following code in my page.tpl.php of a Drupal 7 installation: 我在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];
    }
}

But on some pages in the admin interface, I get: 但是在管理界面的某些页面上,我得到:

Notice: Undefined offset: 2 in include() (line 36 of /home/www/doc/7622/sdd.de/testkc/sites/all/themes/sdd2015/page.tpl.php). 注意:未定义的偏移量:include()中的2(/home/www/doc/7622/sdd.de/testkc/sites/all/themes/sdd2015/page.tpl.php的第36行)。

The code should only be executed if I am on the frontend...? 仅当我在前端时才应执行代码...? Why does it get fired in the backend? 为什么它在后端被解雇?

At a guess: 猜测:

The message is in fact generated when you visit a page on the front end, but because you have this code in page.tpl.php (where it shouldn't be), it can't be printed in the messages area until the next page view, as the messages for the current page having already been printed in the same file. 该消息实际上是在您访问前端页面时生成的,但是由于您在page.tpl.php中存储了此代码(不应在此位置),因此直到下一个页面时 ,它才能在消息区域中打印。页面视图,因为当前页面的消息已经打印在同一文件中。

So your code is probably working as expected, but you're seeing a delay on the notice messages being output. 因此,您的代码可能按预期工作,但是您看到通知消息输出出现延迟。

As always, the solution is to check your vars before using them. 与往常一样,解决方案是在使用前检查您的var。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM