phpdrupal-7drupal-modulesdrupal-views

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. 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. 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.


Solution

  • I found a solution, but I'm not sure if this is the correct drupal way. I used the hook_node_view function to get all nodes that are printed on that page. 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 )

    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);
        }
      }
    }