daily pastebin goal
53%
SHARE
TWEET

Untitled

a guest Dec 16th, 2017 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Get list of options for current view, only at runtime.
  3.  */
  4. protected function get_oids() {
  5. //...
  6. // Sort values.
  7.   $sort_option = $this->options['selective_display_sort'];
  8.   switch($sort_option) {
  9.     case 'WEIGHT':
  10.       $terms = taxonomy_term_load_multiple(array_keys($oids));
  11.       usort($terms, function($a, $b) {
  12.         return $a->weight - $b->weight;
  13.       });
  14.       $oids_ordered_by_weight = array();
  15.       foreach ($terms as $term) {
  16.         $oids_ordered_by_weight["$term->tid"] = $oids[$term->tid];
  17.       }
  18.       $oids = $oids_ordered_by_weight;
  19.       break;
  20.   }
  21.    
  22. if ($bef_nested) {
  23.   $display_options['bef_ul'] = t('Nested Checkboxes/Radio Buttons');
  24. }
  25.    
  26. /* line 314 */
  27. if ($filter instanceof views_handler_filter_selective) {
  28.   $bef_nested = TRUE;
  29. }
  30. /* line 493 */
  31. if ($bef_nested) {
  32.   //...
  33.   $bef_options[$label]['more_options']['bef_select_all_none_nested'] = array(
  34.     '#disabled' => !$filter instanceof views_handler_filter_selective &&(!$filter->options['expose']['multiple'] || !$filter->options['hierarchy']),
  35.    
  36. case 'HCHY':
  37.   $options = array();
  38.   //get remaining terms tids
  39.   $remaining_terms = taxonomy_term_load_multiple(array_keys($oids));
  40.   $tids = array();
  41.   foreach ($remaining_terms as $term) {
  42.     $tids[$term->tid] = $term->tid;
  43.   }
  44.   //get vocabulary tree
  45.   $anyterm = array_shift($remaining_terms);
  46.   $tree = taxonomy_get_tree($anyterm->vid);
  47.   if ($tree) {
  48.     //we flag the terms to avoid displaying them more than once
  49.     $flagged = array();
  50.     //we keep tree items if they match with current selection
  51.     foreach ($tree as $trm) {
  52.       if(in_array($trm->tid, $tids ) && !in_array($trm->tid, $flagged) ){
  53.         //so we always keep its parents displayed
  54.         $parents_all = taxonomy_get_parents_all($trm->tid);
  55.  
  56.         if($anyterm->vid == '3'){//country vocab, Three levels taxonomy
  57.           switch (count($parents_all)) {
  58.             case 3:
  59.               $parent = $parents_all[1];
  60.               $gd_parent = $parents_all[2];
  61.               break;
  62.             case 2:
  63.               $gd_parent = $parents_all[1];
  64.               break;
  65.             default:
  66.               break;
  67.           }
  68.           //Add the gd_parent to options
  69.           if(!in_array($gd_parent->tid, $flagged) && !is_null($gd_parent->tid)){
  70.  
  71.             $gd_parent_depth = get_term_depth($gd_parent->tid)-1;
  72.             $choice = new stdClass();
  73.             $choice->option = array($gd_parent->tid => str_repeat('-', $gd_parent_depth) . $gd_parent->name);
  74.             $options[] = $choice;
  75.             $flagged[] = $gd_parent->tid;
  76.           }  
  77.           //Add the parent to options
  78.           if(!in_array($parent->tid, $flagged) && !is_null($parent->tid)){
  79.  
  80.             $parent_depth = get_term_depth($parent->tid)-1;
  81.             $choice2 = new stdClass();
  82.             $choice2->option = array($parent->tid => str_repeat('-', $parent_depth) . $parent->name);
  83.             $options[] = $choice2;
  84.             $flagged[] = $parent->tid;
  85.           }
  86.         }
  87.  
  88.         //last, add the term itself to options
  89.         $choice3 = new stdClass();
  90.         $choice3->option = array($trm->tid => str_repeat('-', $trm->depth) . $trm->name);
  91.         $options[] = $choice3;  
  92.         $flagged[] = $trm->tid;
  93.       }
  94.     }
  95.   }
  96.   $oids = $options;
  97.  
  98. /*
  99.  * Returns a term depth (https://www.drupal.org/node/886526#comment-7708315)
  100.  */
  101. function get_term_depth($tid) {
  102.   $limit = 5;
  103.   $depth = 0;
  104.   while ($parent = db_select('taxonomy_term_hierarchy', 't') ->condition('tid', $tid, '=') ->fields('t') ->execute() ->fetchAssoc()) {
  105.     $depth ++;
  106.     $tid = $parent['parent'];
  107.     if ($depth > $limit) {
  108.       break;
  109.     }
  110.   }
  111.   return $depth;
  112. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top