如何获取 Drupal 的当前视图/页面标识符?
我正在寻找的是一个 page_id/view_id ,我可以用它来识别和设置特定页面的样式。我会使用标题或网址,但如果上级决定该页面不应再称为 Golf,而是称为 Tee-Time,因为他更喜欢它,则它有可能会更改。
如果当前页面是分页视图(页面 1,2,3,4...),则该标识符可能不会更改。
What I am looking for is a page_id/view_id that I can use to identify and style specific pages. I would use the title or the url, but there is a chance that it could change if the a higher-up decides that the page should no longer be called Golf, but rather Tee-Time because he likes it better.
Presumably this identifier would not change if the current page were to be a paged view (page 1,2,3,4...).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(2)
解决这个问题的一种方法如下。它取决于 url,因此如果它发生变化,类名也会发生变化。
在我的主题 template.php 中,我实现了 hook_preprocess_page:
然后在主页模板中:
这不是通用解决方案。因此,您可能必须根据您的特定需求进行自定义。例如,它需要一些调整才能与路径自动很好地配合。
One way of solving this is the following. It's depending on the url, so if it changes, so does the class-name.
In my themes template.php I implemented hook_preprocess_page:
Then in the main page-template:
This isn't a generic solution. So you'll probably have to customize this for your specific needs. It will for example need som tweaking to play nicely with path auto.
这在一定程度上取决于您的网站的组合方式(面板页面、视图页面、“普通”页面)。本质上,您需要弄清楚哪些变量在范围内,然后确定可以使用其中的哪些信息。要确定范围内的内容,您可以使用 print_r(array_keys(get_define_vars())); ,然后查看各个变量。
一个选项是在
theme_preprocess_page
中执行某些操作。一种选择是通过 page_manager_get_current_page() 获取页面数据,在其中查看,然后根据需要添加主体类。在不知道自己在做什么的情况下,您本质上需要在某个地方print_r
结果,查看您拥有的内容,然后从那里开始。This depends a little on how your site is put together (panel pages, view pages, "normal" pages). Essentially, you need to figure out what vars are in scope, and then determine which information in them can be used. To determine what is in scope, you can use
print_r(array_keys(get_defined_vars()));
and then poke around in the individual vars.An option is to do something in
theme_preprocess_page
. One option is to get the page data viapage_manager_get_current_page()
, poke around in there, and then add body classes as needed. Without knowing what you are doing, you essentially need toprint_r
the results somewhere, look at what you have, and go from there.