简体   繁体   English

根据域,按分类术语筛选Drupal视图(在面板中显示)

[英]Filter a Drupal view (displayed in a panel) by taxonomy term depending on domain

This is a Views 6.x-2.x problem: On a site with many different views (many of which are blocks included in Panels that pass arguments to blocks) I would like to filter views by a taxonomy term depending on the domain the site is visited through. 这是一个Views 6.x-2.x问题:在一个有很多不同视图的网站上(其中很多都是面板中包含的块传递给块的参数)我想根据域的分类术语过滤视图网站是通过访问。 This filtering should be additional to a first argument (taxonomy term). 此过滤应该是第一个参数(分类术语)的补充。

The site is configured to work with different domains, let's say example1.com and example2.com. 该站点配置为使用不同的域,例如example1.com和example2.com。 I want to "connect" those domains to the taxonomy terms 45 and 115. 我想将这些域“连接”到分类法术语45和115。

So for example: 例如:

example1.com/my_view/1 Should show all nodes that have term 1 and term 45. example1.com/my_view/1应显示具有术语1和术语45的所有节点。

example2.com/my_view/1 Should show all nodes that have term 1 and term 115. example2.com/my_view/1应显示具有术语1和术语115的所有节点。

My approach was to add a second argument (the first is the default taxonomy term ID argument). 我的方法是添加第二个参数(第一个是默认的分类术语ID参数)。 As default argument I use the following snipped in the argument handling code: 作为默认参数,我在参数处理代码中使用以下剪切:

<?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;
?>

This works when I use a page display with the path my_view/% (making only the first argument mandatory). 当我使用路径my_view /%(仅强制第一个参数)的页面显示时,这种方法有效。 But when I use it in a panel, I just get an empty view (if "no context" is chosen) or the second argument doesn't have any effect (if "term id of first/all term" is chosen). 但是当我在面板中使用它时,我只得到一个空视图(如果选择“无上下文”)或第二个参数没有任何效果(如果选择“第一个/所有术语的术语ID”)。

Any ideas what could be wrong? 什么想法可能是错的? I have really tried a lot. 我真的尝试了很多。

If you have a custom module you can use hook_views_query_alter . 如果您有自定义模块,则可以使用hook_views_query_alter You basically pick out the "where" clause that's almost doing what you want it to and override it with your custom criteria. 您基本上选择了几乎按照您的意愿执行的“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)";
    }
  }
}

You'll have to examine the $query object to determine which clause to override and the names of the variables involved - uncomment the drupal_set_message line to see it. 您必须检查$ query对象以确定要覆盖的子句和所涉及的变量的名称 - 取消注释drupal_set_message行以查看它。 This technique allows you to do all sorts of tricky exceptions that wouldn't be possible with Views alone. 这种技术允许您执行各种棘手的异常,而这些异常是仅使用Views无法实现的。 Clear your cache after you put this code in your module. 将此代码放入模块后清除缓存。

As I found out here , views ignores the second argument if the first is not present. 正如我在这里发现的那样,如果第一个参数不存在,则视图会忽略第二个参数。 So setting the following default argument for the first taxonomy argument solves the problem, although it's more of a workaround than a real solution: 因此,为第一个分类法参数设置以下默认参数可以解决问题,尽管它更像是一种解决方法,而不是一个真正的解决方案:

if (arg(0) != 'taxonomy') {
  return 'all';
} else {
  return arg(2);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM