holdmann

views_pre_view implementation

Apr 11th, 2013
193
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /**
  4.  * Implements hook_view_pre_view().
  5.  */
  6. function csettings_views_pre_view(&$view, &$display_id, &$args) {
  7. //function csettings_views_pre_build(&$view) {
  8.   if ($view->name == 'catalog' && $view->current_display == 'page') {
  9.     $views_object_types = views_object_types();
  10.   //kpr($view);
  11.   //kpr($view->display_handler->get_handlers('filter'));
  12.   //kpr($views_object_types);
  13.   //$view_filters = $view->display['page']->handler->get_option($views_object_types['filter']['plural']);
  14.   //kpr($view_filters);
  15.     //kpr($view->display['page']->handler); // ->options['filters'] ФИЛЬТРА ТУТ
  16.  
  17.     if (isset($view->args) && count($view->args) == 1) { // It should be taxonomy term tid :)
  18.       $term = taxonomy_term_load(reset($view->args));
  19.       if ($term === FALSE) {
  20.         return;
  21.       }
  22.    
  23.       $possible_node_types = array();
  24.    
  25.       // Iterate all node types
  26.       foreach(array_keys(node_type_get_names()) as $node_type) {
  27.         $instances = field_info_instances('node', $node_type);
  28.         $has_product_reference = FALSE;
  29.         $has_term_reference = FALSE;
  30.         $field_api_fields = array();
  31.  
  32.         // Iterate all instancies searching for all posible fields
  33.         foreach ($instances as $instance) {
  34.           $field_info = field_info_field($instance['field_name']);
  35.           // use only FIELD_LOAD_CURRENT: FIELD_LOAD_REVISION consists same fields. differs only table name...
  36.           foreach($field_info['storage']['details']['sql']['FIELD_LOAD_CURRENT'] as $sql_table => $sql_fields) {
  37.             foreach($sql_fields as $sql_field) {
  38.               $field_api_fields[] = $sql_field;
  39.             }
  40.           }
  41.           if ($field_info['type'] == 'taxonomy_term_reference') {
  42.             foreach ($field_info['settings']['allowed_values'] as $vocabulary) {
  43.               if ($vocabulary['vocabulary'] == $term->vocabulary_machine_name) {
  44.                 $has_term_reference = TRUE;
  45.                 $instances = field_info_instances('taxonomy_term', $term->vocabulary_machine_name);
  46.                 foreach ($instances as $instance) {
  47.                   $field_info = field_info_field($instance['field_name']);
  48.                   foreach($field_info['storage']['details']['sql']['FIELD_LOAD_CURRENT'] as $sql_table => $sql_fields) {
  49.                     foreach($sql_fields as $sql_field) {
  50.                       $field_api_fields[] = $sql_field;
  51.                     }
  52.                   }
  53.                 }
  54.               }
  55.             }
  56.           }
  57.           if ($field_info['type'] == 'commerce_product_reference') {
  58.             $has_product_reference = TRUE;
  59.             foreach($instance['settings']['referenceable_types'] as $product_type => $v) {
  60.               if ($product_type === $v) {
  61.                 $instances = field_info_instances('commerce_product', $product_type);
  62.                 foreach ($instances as $instance) {
  63.                   $field_info = field_info_field($instance['field_name']);
  64.                   foreach($field_info['storage']['details']['sql']['FIELD_LOAD_CURRENT'] as $sql_table => $sql_fields) {
  65.                     foreach($sql_fields as $sql_field) {
  66.                       $field_api_fields[] = $sql_field;
  67.                     }
  68.                   }
  69.                 }
  70.               }
  71.             }
  72.           }
  73.  
  74.           if ($has_term_reference && $has_product_reference) {
  75.             $possible_node_types[$node_type] = $field_api_fields;
  76.           }
  77.         }
  78.       }
  79.    
  80.       $views_object_types = views_object_types();
  81.       $view_filters = $view->display['page']->handler->get_option($views_object_types['filter']['plural']);
  82.    
  83.       $view_filters1 = array();
  84.       // kpr($view);
  85.    
  86.    
  87.       foreach ($possible_node_types as $node_type => $field_api_fields) {
  88.         foreach($view_filters as $view_field => $filter) {
  89.           if (isset($filter['exposed']) && $filter['exposed'] && !in_array($filter['field'], $field_api_fields)) {
  90.             //unset($filter);
  91.             //unset($view->display['page']->handler->options['filters'][$view_field]);
  92.             //$view->display['page']->handler->options['filters'][$view_field]['exposed'] = FALSE;
  93.            
  94.             // MY PREVIOUS TRY
  95.             /*
  96.             unset($view->display[$view->current_display]->display_options['filters'][$view_field]);
  97.             unset($view->display[$view->current_display]->handler->options['filters'][$view_field]);
  98.             unset($view->display_handler->display->display_options['filters'][$view_field]);
  99.             unset($view->display_handler->options['filters'][$view_field]);
  100.             */
  101.            
  102.             // YOUR SUGGESTION
  103.             //$view->display_handler->handlers['filter'][$filter_field]->options['exposed'] = FALSE;
  104.  
  105.             // PRE EXECUTE
  106.             //unset($view->display_handler->handlers['filter'][$view_field]);
  107.             //unset($view->filter[$view_field]);
  108.           } else {
  109.             $view_filters1[$view_field] = $filter;
  110.           }
  111.         }
  112.       }
  113.    
  114.       //kpr($view_filters);
  115.       //kpr($view_filters1);
  116.       //$view->display_handler->override_option('filters', $view_filters1);
  117.    
  118.       // kpr($view->display['page']->handler->options);
  119.       //kpr($possible_node_types);
  120.       //kpr($view_filters);
  121.       //kpr($view->display['page']->handler); // ->get_option($views_object_types['filter']['plural']
  122.       // kpr($view);
  123.     }
  124.   }
  125. }
RAW Paste Data