简体   繁体   English

drupal7根据视图中的会话过滤内容类型

[英]drupal7 Filter content type based on session in view

I have created a custom type with multiple fields. 我创建了一个具有多个字段的自定义类型。 1 field is a checkbox to "show for all people" 2nd field is a textfield ( you can add multiple textfields ) for adding a code. 第一个字段是“向所有人显示”的复选框。第二个字段是用于添加代码的文本字段(可以添加多个文本字段)。

I created a view where all those content types are being shown in a page. 我创建了一个视图,其中所有这些内容类型都在页面中显示。 ( this works ) (这有效)

But now: When a person enters the site, he has to insert a code. 但是现在:当一个人进入站点时,他必须插入一个代码。 This code is saved into a cookie because it needs to be remembered for about 2 weeks. 该代码被保存到cookie中,因为它需要记住大约2周。 So I can't use the contextual filters. 所以我不能使用上下文过滤器。

If the checkbox "show for all people" is checked, this block is shown. 如果选中了“向所有人显示”复选框,则会显示此块。 if the checkbox "show for all people" is unchecked, this block is hidden, except for people who came in without a code, or if the code is one of the values that was inserted in the 2nd field. 如果未选中“为所有人显示”复选框,则该块将被隐藏,除了没有代码的人,或者代码是第二个字段中插入的值之一。

I don't wan't to use views php_filter. 我不要使用php_filter视图。 But I have no clue how to proceed with this problem. 但是我不知道如何解决这个问题。

I tried some solutions on the web to create a custom filter, but the problem here is, that we can't access the form values. 我在网络上尝试了一些解决方案来创建自定义过滤器,但是这里的问题是,我们无法访问表单值。

I found a solution, but I'm not sure if this is the correct drupal way. 我找到了解决方案,但是我不确定这是否是正确的drupal方法。 I used the hook_node_view function to get all nodes that are printed on that page. 我使用了hook_node_view函数来获取打印在该页面上的所有节点。 I check if the code that was inserted into a cookie with the codes that are allowed ( created in the text fields of the content type ) 我检查是否使用允许的代码(在内容类型的文本字段中创建)插入了Cookie的代码

function code_node_view($node, $view_mode, $langcode) {
  if ($node->type == 'winning_codes') {
    $code = _code_read_cookie('code');
    $winning_codes = (!empty($node->field_winning_codes['und'])) ? $node->field_winning_codes['und'] : array();
    $winning_codes = array_map(function ($ar) {
      return $ar['value'];
    }, $winning_codes);
    if (!empty($code) && (!in_array($code, $winning_codes))) {
      hide($node->content);
    }
  }
}

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

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