Hi,

I'm not sure if this is possible at all but what I'm trying to achieve is the following.

I have an ajax enabled view blok which I place with certain nodes.
Now when the node is first loaded I would like to filter the view based on the taxonomy terms given to that node.
But also give userss the option to change the exposed filter and list based upon the terms they've chosen.

My first thought was to have an exposed filter with term ID options and to create a argument with the following settings;
- Action to take if argument is not present: Provide default argument
- PHP Code:

$node=node_load(arg(1));
if($node) {
	$terms = array();
	foreach($node->taxonomy as $term){
		if($term->vid == 2){
			$terms[]=$term->tid;
		}
	}
	return implode('+', $terms);
} else {
	return FALSE;
}

Yet when the exposed form is submitted the view still has no real argument so the 'action to take...' kicks in.
This results in queries like;

WHERE (term_node.tid = 14) AND (node.type in ('page')) AND (term_node2.tid = 11)

tid 14 is taken from the exposed form
tid 11 is taken from the node

Is there a way to only use the term taken from the node if the exposed filter wasn't set?

Thank you very much for any help!
Cheers

Comments

AntiNSA’s picture

subscribe, me too

bartezz’s picture

Ok I found a solution for my case, this might not be a solution for yours but I'll post it just in case.

I have a block VIEW with an exposed filter for the field [Taxonomy Term ID].
I attach these block views to certain nodes as you normally do (admin/build/block/configure/)

Now in my case I wanted the block view to only list nodes that have a similar term as the node to which the view was attached.
I set the view up as you'd normally do and

- created an exposed filter for [Taxonomy Term ID]
- Filter identifier: MY_FILTER_NAME

Then I:

- created an argument for the field [Taxonomy: Term ID]
- Action to take if argument is not present: Provide default argument
- Default argument type: PHP code
- in the PHP field I added the following code

if($_GET['MY_FILTER_NAME'] AND $_GET['MY_FILTER_NAME'] != 'All') {
	$ret = $_GET['MY_FILTER_NAME'];
	unset($_GET['MY_FILTER_NAME']);
	return $ret;
}elseif($_GET['MY_FILTER_NAME'] == 'All') {
	$tree = taxonomy_get_tree(2);   // THE VID OF THE TAXONOMY HERE
	$terms = array();
	foreach($tree as $term) {
		$terms[]=$term->tid;
	}
	return implode('+',$terms);
}
if($_GET['MY_FILTER_NAME'] != 'All') {
	$node=node_load(arg(1));
	if($node) {
		$terms = array();
		foreach($node->taxonomy as $term){
			if($term->vid == 2){    // THE VID OF THE TAXONOMY HERE
				$terms[]=$term->tid;
			}
		}
		return implode('+',$terms);
	} else {
		return FALSE;
	}
} 
return FALSE;

- checked: Allow multiple terms per argument.
- checked: Allow multiple arguments to work together.
- checked: Reduce duplicates

So far so good. Now there is still one problem, Views will post the argument given the first time as a hidden form field with the exposed filters.
So I've created a views-exposed-form--VIEWNAME.tpl.php and added the next jQuery

<script language="javascript" type="text/javascript">
<!--//
$('.views-exposed-widget .form-submit').hover(function(){
	$('input[name=view_args]').val('');												   
});
//-->
</script>

This will empty the value of the hidden form field, that way when an exposed filter is submitted it will forget about the arguments.

Hope this helps others!
Cheers

merlinofchaos’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.