From 6338081baed6b3495cc91990239a15de76112331 Mon Sep 17 00:00:00 2001
From: git <git@1053618.no-reply.drupal.org>
Date: Sun, 16 Nov 2014 01:36:01 +0100
Subject: [PATCH] Pass arguments to views filter

---
 term_reference_tree.widget.inc | 63 ++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 60 insertions(+), 3 deletions(-)

diff --git a/term_reference_tree.widget.inc b/term_reference_tree.widget.inc
index 1685a43..286b5fb 100644
--- a/term_reference_tree.widget.inc
+++ b/term_reference_tree.widget.inc
@@ -16,6 +16,7 @@ function term_reference_tree_field_widget_info() {
         'start_minimized' => 0,
         'leaves_only' => 0,
         'filter_view' => '',
+        'filter_view_args' => '',
         'select_parents' => 0,
         'cascading_selection' => 0,
         'track_list' => 0,
@@ -28,6 +29,51 @@ function term_reference_tree_field_widget_info() {
 }
 
 /**
+ * Themes the term tree display (as opposed to the select widget).
+ */
+function theme_term_tree_list($variables) {
+  $element =& $variables['element'];
+  $data =& $element['#data'];
+
+  $tree = array();
+
+  # For each selected term:
+  foreach($data as $item) {
+    # Loop if the term ID is not zero:
+    $values = array();
+    $tid = $item['tid'];
+    $original_tid = $tid;
+    while($tid != 0) {
+      # Unshift the term onto an array
+      array_unshift($values, $tid);
+      
+      # Repeat with parent term
+      $tid = _term_reference_tree_get_parent($tid);
+    }
+    
+    $current =& $tree;
+    # For each term in the above array:
+    foreach($values as $tid) {
+      # current[children][term_id] = new array
+      if (!isset($current['children'][$tid])) {
+        $current['children'][$tid] = array('selected' => FALSE);
+      }
+      
+      # If this is the last value in the array, tree[children][term_id][selected] = true
+      if ($tid == $original_tid) {
+        $current['children'][$tid]['selected'] = TRUE;
+      }
+
+      $current['children'][$tid]['tid'] = $tid;
+      $current =& $current['children'][$tid];
+    }
+  }
+  
+  return _term_reference_tree_output_list_level($element, $tree);
+}
+
+
+/**
  * Implements hook_field_widget_settings_form().
  */
 function term_reference_tree_field_widget_settings_form($field, $instance) {
@@ -89,6 +135,14 @@ function term_reference_tree_field_widget_settings_form($field, $instance) {
         '#default_value' => $settings['filter_view'],
         '#options' => $options,
       );
+      
+      $form['filter_view_args'] = array(
+        '#type' => 'textfield',
+        '#title' => 'Arguments passed to the view',
+        '#description' => t("Arguments that will be passed to the view. Multiple arguments must be seperated by a comma. ") . (module_exists("token") ? t("You can use global tokens from the list below. ") : ""),
+        '#default_value' => $settings['filter_view_args'],
+      );
+      
     }
     else {
       $form['filter_view'] = array(
@@ -274,7 +328,9 @@ function term_reference_tree_process_checkbox_tree($element, $form_state) {
     $allowed = '';
 
     if ($element['#filter_view'] != '') {
-      $allowed = _term_reference_tree_get_allowed_values($element['#filter_view']);
+      $args = (module_exists('token')) ? token_replace($element['#filter_view_args']) : $element['#filter_view_args'];
+      $args = explode(",", $args);
+      $allowed = _term_reference_tree_get_allowed_values($element['#filter_view'], $args);
     }
 
     $value = !empty($element['#default_value']) ? $element['#default_value'] : array();
@@ -511,6 +567,7 @@ function term_reference_tree_field_widget_form(&$form, &$form_state, $field, $in
       $element['#start_minimized'] = $settings['start_minimized'];
       $element['#leaves_only'] = $settings['leaves_only'];
       $element['#filter_view'] = module_exists('views') ? $settings['filter_view'] : '';
+      $element['#filter_view_args'] = module_exists('views') ? $settings['filter_view_args'] : '';
       $element['#select_parents'] = $settings['select_parents'];
       $element['#cascading_selection'] = $settings['cascading_selection'];
       $element['#track_list'] = $settings['track_list'];
@@ -595,7 +652,7 @@ function _term_reference_tree_widget_validate(&$element, &$form_state) {
  * @return
  *   An array of term IDs (tid => true) returned by the view.
  */
-function _term_reference_tree_get_allowed_values($filter) {
+function _term_reference_tree_get_allowed_values($filter, $args) {
   $viewname = "";
   $displayname = "";
   $allowed = array();
@@ -607,7 +664,7 @@ function _term_reference_tree_get_allowed_values($filter) {
       if ($view->access($displayname)) {
         // Save the page title first, since execute_display() will reset this to the display title.
         $title = drupal_get_title();
-        $view->execute_display($displayname);
+        $view->execute_display($displayname, $args);
         $title = drupal_set_title($title, PASS_THROUGH);
         foreach ($view->result as $item) {
           $allowed[$item->tid] = TRUE;
-- 
1.9.1

