如何获取 Drupal 的当前视图/页面标识符?

发布于 2024-09-30 11:00:35 字数 163 浏览 6 评论 0原文

我正在寻找的是一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红尘作伴 2024-10-07 11:00:35

解决这个问题的一种方法如下。它取决于 url,因此如果它发生变化,类名也会发生变化。

在我的主题 template.php 中,我实现了 hook_preprocess_page:

function mytheme_preprocess_page(&$vars, $hook) {
  $body_classes = array();
  $body_classes[] = 'page-' . _get_page_name($_SERVER['REQUEST_URI']);
  $vars['body_classes'] = implode(' ', $body_classes);
}

function _get_page_name($request_uri) {
  static $numeric_subsection = array(
    '/node/' => 'node',
  );

  $preAlias = $request_uri;
  $alias = substr(strrchr($preAlias, "/"), 1);
  if (strpos($alias, '?') > -1) {
    $alias = substr($alias, 0, strpos($alias, '?'));
  }

  $page_name = $alias;
  if (empty($alias)) {
    $page_name = 'start';
  }
  else if (is_numeric($alias)) {
    foreach ($numeric_subsection as $section => $pn) {
       if (strpos($preAlias, $section) > -1) {
         $page_name = $pn;
       }
    }
  }

  return $page_name;
}

然后在主页模板中:

<body class="<?php print $body_classes; ?>">

这不是通用解决方案。因此,您可能必须根据您的特定需求进行自定义。例如,它需要一些调整才能与路径自动很好地配合。

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:

function mytheme_preprocess_page(&$vars, $hook) {
  $body_classes = array();
  $body_classes[] = 'page-' . _get_page_name($_SERVER['REQUEST_URI']);
  $vars['body_classes'] = implode(' ', $body_classes);
}

function _get_page_name($request_uri) {
  static $numeric_subsection = array(
    '/node/' => 'node',
  );

  $preAlias = $request_uri;
  $alias = substr(strrchr($preAlias, "/"), 1);
  if (strpos($alias, '?') > -1) {
    $alias = substr($alias, 0, strpos($alias, '?'));
  }

  $page_name = $alias;
  if (empty($alias)) {
    $page_name = 'start';
  }
  else if (is_numeric($alias)) {
    foreach ($numeric_subsection as $section => $pn) {
       if (strpos($preAlias, $section) > -1) {
         $page_name = $pn;
       }
    }
  }

  return $page_name;
}

Then in the main page-template:

<body class="<?php print $body_classes; ?>">

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.

可遇━不可求 2024-10-07 11:00:35

这在一定程度上取决于您的网站的组合方式(面板页面、视图页面、“普通”页面)。本质上,您需要弄清楚哪些变量在范围内,然后确定可以使用其中的哪些信息。要确定范围内的内容,您可以使用 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 via page_manager_get_current_page(), poke around in there, and then add body classes as needed. Without knowing what you are doing, you essentially need to print_r the results somewhere, look at what you have, and go from there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文