drupal - 根据域按分类术语过滤 Drupal View (显示在面板中)

标签 drupal drupal-6 drupal-views drupal-panels

这是一个 Views 6.x-2.x 问题:在具有许多不同 View 的站点上(其中许多是包含在面板中的 block ,它们将参数传递给 block )我想根据域按分类术语过滤 View 网站访问通过。此过滤应附加到第一个参数(分类术语)。

该站点配置为使用不同的域,例如 example1.com 和 example2.com。我想将这些域“连接”到分类术语 45 和 115。

例如:

example1.com/my_view/1
应显示具有术语 1 和术语 45 的所有节点。

example2.com/my_view/1
应显示具有术语 1 和术语 115 的所有节点。

我的方法是添加第二个参数(第一个是默认的分类术语 ID 参数)。作为默认参数,我在参数处理代码中使用以下片段:

<?php
// Get domain.
$host = preg_match('/[^.]+\.[^.]+$/', $_SERVER['HTTP_HOST'], $hit);
$host = $hit[0];

// Select taxonomy term.
if ($host == 'example1.com'){
  $taxonomy = '45';
} elseif ($host == 'example2.com'){
  $taxonomy = '115';
}

return $taxonomy;
?>

这在我使用带有路径 my_view/% 的页面显示时有效(仅使第一个参数成为强制性)。但是当我在面板中使用它时,我只会得到一个空 View (如果选择了“无上下文”)或第二个参数没有任何效果(如果选择了“第一个/所有术语的术语 ID”)。

有什么想法可能是错的吗?我真的尝试了很多。

最佳答案

如果您有自定义模块,您可以使用 hook_views_query_alter .您基本上选择了几乎在做您想做的事情的“where”子句,并用您的自定义条件覆盖它。

function [custom module name]_views_query_alter(&$view, &$query) {
  // pick out the right View by its name
  if($view->name == "[your View's machine name]"){

    // drupal_set_message(print_r($query->where, 1)); // Uncomment to see "where" array

    // Get domain.
    $host = preg_match('/[^.]+\.[^.]+$/', $_SERVER['HTTP_HOST'], $hit);
    $host = $hit[0];

    // Change the taxonomy term dependent on host
    if ($host == 'example1.com'){
      $query->where[0]['clauses'][2] = "(term_node_value_1.tid = 45)";
    } elseif ($host == 'example2.com'){
      $query->where[0]['clauses'][2] = "(term_node_value_1.tid = 115)";
    }
  }
}

您必须检查 $query 对象以确定要覆盖的子句以及所涉及变量的名称 - 取消注释 drupal_set_message 行以查看它。这种技术允许您执行各种单独使用 View 无法实现的棘手异常。将此代码放入模块后清除缓存。

关于drupal - 根据域按分类术语过滤 Drupal View (显示在面板中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20248929/

相关文章:

django - 切换 CMS 的工具和技巧

twig - 匹配子串是 Twig

drupal ctools - 如何在模态中显示 View

php - 为什么 Drupal 表单不会缓存在 cache_form 中?

drupal - 模块卸载清理

Drupal:列出内容类型和内容计数

c# - 使用 C# 在 Drupal 中创建节点

php - Drupal 有哪些不足之处?

sql - 在 Git 中存储 Drupal SQL

drupal - 将日期数组保存到数据库