如何按 URL 中的分类参数过滤块视图?

发布于 2024-09-30 14:27:55 字数 791 浏览 6 评论 0原文

我的侧边栏中有一些块视图,其中显示了标记为某些城市发生的亮点的事件。使用分类法将节点组织成城市。

当我直接加载节点时,我有一个像 www.host.com/events/new-york/name-of-my-nice-event 这样的 URL,

我还有一些其他页面视图,可以显示所有内容的预告片某个城市的活动:www.host.com/events/new-york

另外,我还有一些对所有城市都有效的静态页面,例如www.host.com/about-us

显示亮点的侧边栏块在整个网站中都可用。现在,我想确保侧边栏中的块仅根据 URL 中提供的分类显示所选城市的节点。 (静态页面除外,因为 URL 中没有分类,但这些并不那么重要)

到目前为止,我尝试使用 PHP 作为标准参数将分类术语作为参数传递:

if (arg(1)) {
    $term = arg(1);
    return $term;
}

这在上述页面上运行良好视图(例如www.host.com/events/new-york)。但是,当我直接加载节点时,我的块仅显示空文本。

我认为参数的索引是这样的:

           events/new-york/name-of-my-nice-event
           ^0     ^1       ^2

所以我不明白为什么当我查看节点详细信息时 arg(1) 不返回 new-york 。

I have some block views in my sidebar that show events marked as a highlight happening in certain cities. Nodes are organized into cities using taxonomy.

When I load a node directly I have an URL like www.host.com/events/new-york/name-of-my-nice-event

I have some other page views that show teasers for all events in a certain city: www.host.com/events/new-york

Also I have some static pages that are valid for all cities, e.g. www.host.com/about-us

The sidebar blocks showing the highlights are available throughout the whole website. Now I want to make sure that the blocks in my sidebar only show those nodes for the selected city based on the taxonomy provided in the URL. (except for the static pages as there is no taxonomy in the URL, but those are not that important)

So far I tried to pass my view the taxonomy term as an argument using PHP as standard argument:

if (arg(1)) {
    $term = arg(1);
    return $term;
}

This works fine on the above mentioned page views (e.g. www.host.com/events/new-york). But when I load a node directly www.host.com/events/new-york/name-of-my-nice-event my block only shows the empty text.

I thought that arguments are indexed like this:

           events/new-york/name-of-my-nice-event
           ^0     ^1       ^2

So I don't understand why arg(1) does not return new-york when I am viewing the node detail.

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

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

发布评论

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

评论(3

逆蝶 2024-10-07 14:27:55

首先,使用路径和路径自动,您所看到的并不总是您得到的。

Fx 我可以为我的文章节点设置 pathauto 来生成像这样的 URL

content/article/[title]

所以如果我想要标题,我应该使用 arg(2) 对吗?

不!(在这种情况下,arg(2) 实际上是 NULL。)

原因是由 path auto 生成的 url 是假的url,被转换为 Drupal url。在上面的例子中,我得到的是node/[nid]。因此,尽管url中的节点标题i,我无法使用arg()获取它,但我可以使用arg(1)获取nid

我无法猜测你的网址映射到什么,这取决于你如何设置你的网站,你使用什么模块等。

如果你做了很多这些上下文感知的事情,一个好的建议是研究< a href="http://drupal.org/project/panels" rel="nofollow">面板。它能够告诉诸如视图之类的模块它所存在的上下文。像 fx 术语、节点等,您可以使用它来将参数传递到视图中。

面板可以做更多的事情并且相当复杂,但是如果您需要做很多这样的事情,那么它可能是值得投资的。

First of all, with path and path auto what you see is not always what you get.

Fx I could setup pathauto for my articles nodes to generate urls like this

content/article/[title]

So if I wanted the title I should use arg(2) right?

No! (arg(2) is actually NULL in this case.)

The reason is that the url that's generated by path auto is a fake url, that gets translated into a Drupal url. In the case above what I get is node/[nid]. So eventhough the node title i in the url, I can't get it by using arg(), but I can get the nid by using arg(1)

I can't guess what your urls map to, it depends how you have set up your site what modules you use etc.

A good advice if you do a lot of these context aware things, is to look into panels. It's made to be able to tell modules like views about the context which it is present. Like fx terms, nodes, etc, and you could use this to pass arguments into views.

Panels can do a lot more and is quite complex, but if you need to do a lot of this stuff, it is probably worth the investment.

回首观望 2024-10-07 14:27:55

我的问题的解决方案:

if (arg(0) == 'node' && is_numeric(arg(1))) {
    $node = node_load(arg(1));

    if (count($node->taxonomy) > 0)  {
      foreach ($node->taxonomy as $term) {
        $term = $term->name;
      }
    }

    $term = strtolower($term); // for some reason needed in my case
}

else {
    $term = arg(1);
    $term = str_replace('-', ' ', $term); // for some reason needed in my case
}

return $term;

Solution to my problem:

if (arg(0) == 'node' && is_numeric(arg(1))) {
    $node = node_load(arg(1));

    if (count($node->taxonomy) > 0)  {
      foreach ($node->taxonomy as $term) {
        $term = $term->name;
      }
    }

    $term = strtolower($term); // for some reason needed in my case
}

else {
    $term = arg(1);
    $term = str_replace('-', ' ', $term); // for some reason needed in my case
}

return $term;
筑梦 2024-10-07 14:27:55

虽然这在技术上可以通过 Views 2 实现,如其他一些答案中所述,但 Views 3 内置了此集成。

您可以为分类术语 id 创建参数,然后选择“提供默认参数” 。然后,这将为您提供“来自 URL 的分类术语 ID”和“从节点页面加载默认参数...”的选项,

这将允许您获取页面的分类并将其作为您的视图块的参数。

注意:Views 3 目前处于 Alpha 3 阶段,但根​​据我的经验,它处于相对稳定的状态,并且我正在生产站点上使用它。如果它具有您认为有用的上述功能,请使用它、测试它并在遇到任何问题时提交错误/补丁!

While this was technically possible with Views 2 as described in some of the other answers, Views 3 has this integration built in.

You can create an argument for the taxonomy term id and then choose "Provide default argument". This will then give you an option for "Taxonomy Term ID from URL" and "Load default argument from node page..."

This will allow you to take the taxonomy of a page and pass that as an argument to your view block.

Note: Views 3 is currently in Alpha 3 but in my experience is at a relatively stable state and I am using it on production sites. If it has features like the one above that you find useful please use it, test it and submit bugs/patches if you encounter any issues!

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