Hey folks,

as the filtering by a view for selectable terms is already implemented it would be usefull to pass arguments to the view for many purposes.
This could by easily implemented. Unfortunatly, I do not know yet, how to create a patch, therefore I post the elementary code which would pass the arguments to the view.

@file: term_reference_tree.widget.inc
@function: _term_reference_tree_get_allowed_values($filter)
@param $filter: May look like "filter_view:default" and is the value from the widget form $form['#filter_view']. The first part is the machinename of the view, second the display.

On Line 608 the view gets executed by:

  $view->execute_display($displayname);

With the call like

  $view->execute_display($displayname, $args);

we pass arguments to the view, where $args = array().

Let's extend the widget form by a "tokenized-able" field, called "Views-Arguments" ($form['#filter_view_args']), whose value (i.e. [current-user:uid]) gets passed to the function:

function _term_reference_tree_get_allowed_values($filter, $args) {
  $viewname = "";
  $displayname = "";
  $allowed = array();

  if (module_exists('views') && $filter != '') {
    list($viewname, $displayname) = explode(":", $filter);
    $view = views_get_view($viewname);
    if (is_object($view)) {
      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, $args);
        ...
}

The call that needs the be altered is
@function: term_reference_tree_process_checkbox_tree()

function term_reference_tree_process_checkbox_tree($element, $form_state) {
  if (is_array($form_state)) {
    if (!empty($element['#max_choices']) && $element['#max_choices'] != '-1')
      drupal_add_js(array('term_reference_tree' => array('trees' => array($element['#id'] => array('max_choices'=>$element['#max_choices'])))), 'setting');
    $allowed = '';
    
    if ($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);
    }
    ...
}

Furthermore the functions:
@see term_reference_tree_field_widget_settings_form()
@see term_reference_tree_field_widget_form()
@see term_reference_tree_field_widget_info()
have to be adapted.

I have posted the altered file as an attachment for the developers to be aggregated to the project.
I have tested this new feature, please test it by yourself.

Comments

dave reid’s picture

Version: 7.x-2.x-dev » 7.x-1.x-dev
discipolo’s picture

tried to turn this into a patch against the current dev version

discipolo’s picture

i added one funtion to many by accident

vladimiraus’s picture

Assigned: HydroZ » Unassigned
Status: Needs review » Closed (outdated)

Thank you everyone for your contribution.
Drupal 7 is no longer supported but we always need support for Drupal 11 version.
Closing as outdated.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.