如何使用带参数的视图作为 Drupal 中的站点首页?

发布于 2024-10-10 17:39:44 字数 497 浏览 1 评论 0原文

我有一个 Drupal 站点,并且设置了一个视图来支持首页。

我的目标是能够将 0-2 个参数传递到主页,然后传递到视图中。然而,我仍然需要正常的 Drupal 页面才能工作。参数列表是已知的。

例如:

  • mysite.com/berlin/birds 将传递“berlin”作为第一个参数,“birds”作为第二个参数传递给支持首页的视图。
  • mysite.com/berlin 只会传递一个参数,“berlin”
  • mysite.com/admin 会加载 Drupal 中的正常管理页面,

我不清楚如何实现这一点。有我可以使用的钩子吗?我找不到或想不出一个。有没有办法在视图本身的参数中指定这一点?也许我可以编写一个钩子,在加载 URL 时插入,并在后台重写?

我目前的解决方案是将这些路径(因为我的参数已知)添加到菜单系统中。这是可行的,除了当我访问页面时它们不是首页,因此页面不使用我想要的节点主题(它们使用节点详细信息主题)。

I have a Drupal site and I have setup a view to power the front page.

My goal is to be able to pass 0-2 arguments to the home page, that get passed into the view. However, I still need the normal Drupal pages to work. The list of arguments is known.

For example:

  • mysite.com/berlin/birds would pass in "berlin" as the first argument and "birds" as the second argument to the view that powers the front page.
  • mysite.com/berlin would just pass in one argument, "berlin"
  • mysite.com/admin would load the normal admin pages in Drupal

I'm not clear on how to achieve this. Is there a hook I can use? I can't find one or think of one. Is there a way to specify this in the argument for the view itself? Perhaps I can write a hook that interjects when the URL is being loaded, and rewrite in the background?

The solution I currently have is to add these paths (since my arguments are known) to the menu system. This works, except that when I the pages they aren't the front page, so the pages don't use the node themes I want (they use the node details theme).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

梦在深巷 2024-10-17 17:39:44

我认为如果没有自定义代码,您真的无法做到这一点。视图在开始接受参数之前需要一个路径,并且您的 URL 以参数开头。您可以做的就是使用自定义代码来伪造它。我使用过这样的东西:

/**
 * Implements hook_init().
 */
function mymodule_custom_init() {

  $item = menu_get_item(); // Matching Drupal path

  if (!$item) { // There is no matching Drupal path.
    $_GET['q'] = 'view_path/' . $_GET['q']; // Fake path path.
  } // if

} // mymodule_custom_init

然后你给视图一个“view_path”路径,它会响应与 Drupal 路径中其他任何内容不匹配的所有内容。

I don't think you can really do this without custom code. The View needs a path before it starts taking arguments, and your URLs start with the arguments. What you can do is fake it with custom code. I've used something like this:

/**
 * Implements hook_init().
 */
function mymodule_custom_init() {

  $item = menu_get_item(); // Matching Drupal path

  if (!$item) { // There is no matching Drupal path.
    $_GET['q'] = 'view_path/' . $_GET['q']; // Fake path path.
  } // if

} // mymodule_custom_init

Then you give the view a path of "view_path" and it responds to everything that doesn't match anything else in Drupal's paths.

jJeQQOZ5 2024-10-17 17:39:44

视图 UI 中有一个“参数处理代码”的位置,它需要一小段 php - http://drupal .org/node/70145

您可以在那里运行一些代码来检查您是否位于首页(或参数不存在或其他)并插入您想要的参数。

There is a spot in the views UI for "Argument handling code" that takes a small snippet of php - http://drupal.org/node/70145

You could run some code there that checks to see if you are on the front page (or arguments are not present or whatever)and insert the arguments you want.

默嘫て 2024-10-17 17:39:44

另一种方法是通过 hook_views_pre_view 或 hook_views_pre_build 设置参数。更好,因为您确定不会破坏其他东西(例如另一个视图块)。

function MYMODULE_views_pre_view(&$view){
    if ($view->name == 'your_view_name' && drupal_is_front_page()) {
        $view->set_arguments(array('you_argument','you_second_argument'));
    }
}

Another way is to set arguments via a hook_views_pre_view or hook_views_pre_build. Is better because you are sure you don't break other stuff (like another view block).

function MYMODULE_views_pre_view(&$view){
    if ($view->name == 'your_view_name' && drupal_is_front_page()) {
        $view->set_arguments(array('you_argument','you_second_argument'));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文