I created a block that shows nodes of type 'answer' with a given taxonomy term displayed in a sidebar, using Views. I want to filter this block output to exclude the current node - so that you don't see a particular answer in the block when this answer is itself the node being accessed. Any suggestsions?

Thanks!

Comments

francort’s picture

You can have node id of current node like( and know if it is a node):

$url_explode = explode("/", $_GET['q']);
$is_node = FALSE;
if( $url_explode[0] == node ){
       $node_id = $url_explode[1]:
       $is_node = TRUE;
}

check in your if what you're writting is the same node:
<?
if( !( $is_node && $node_id == $node->id) ){
//code if is not the same node
}
else{
//code if is the same node
}

I haven't tried, but maybe it would help

Good luck!

rancky’s picture

Ok,
it may work. But what if I use clean URLs with explicit paths ?
I won't be able to get the node ID from the URL...

francort’s picture

As far as i know, it doesn't matter.
But if you try and it doesn't work, please tell me to research a bit more about it

nevets’s picture

If you use the arg() function it always returns the parts of the unaliased path. So given node/123, arg(0) returns 'node' and arg(1) returns 123.

francort’s picture

better :)

dan_no_yes’s picture

Great - thanks all!

rancky’s picture

I would like to get the current node ID in a view in order to exclude this current node.

How can I do this ? In my block processing or in my view ? Should I use the Views arguments ?

I'm using Views 2 and Drupal 6.6. The module PHPfilter does not work.

nevets’s picture

Use views arguments, assuming this is a block view

  1. Add an argument for Node: nid
  2. Set 'Action to take if argument is not present' to 'Provide default argument'
  3. Set 'Default argument type' to 'Node ID from URL'
  4. Check 'Exclude the argument'
rancky’s picture

Perfect.
Many thanks !

garryh’s picture

I'm still bungling my way through this, but thanks to the tips above, I've had success modifying the Taxonomy Term View by creating a custom See Also: Block which is visible only on node pages. It lists other nodes similarly tagged, but excludes the current node.

For your Block, override the Contextual Filters (Arguments) from the defaults.
Add these filters and arrange them in this order;

Content: Nid
Content: Has taxonomy term ID (with depth)
Content: Has taxonomy term ID depth modifier

For Content: Nid

  • Provide a default PHP value (minus the PHP tags)
if ( arg(0) == 'node' and is_numeric(arg(1)) and arg(2) == FALSE ) {
// we're viewing a node in View mode
return (arg(1));
}
  • Expand MORE, and check the boxes for 'Allow multiple values' and 'Exclude' this value rather than limit the View

For Content: Has taxonomy term ID (with depth)

  • Depth is up to you
  • Check the box 'Allow Multiple Values'. Your PHP below will create the necessary text string n+n+n etc.
  • Provide a Default PHP value (minus the PHP tags)
  • You will need to replace field_yourvocab with the machine name of the Term Reference field
 // FIND MULTIPLE TERMS of the current NODE,
 // first creating an array, and then 
 // a text string of those terms

if ( arg(0) == 'node' and is_numeric(arg(1)) and arg(2) == FALSE ) {
    // Yes, we're viewing a node in view mode.

 $node = node_load(arg(1));
 $output = (''); // initializes $output as a text string

 if( !empty($node->field_yourvocab) ) { // IMPORTANT - Does the taxonomy field exist for this node?
 
	$yourvocab = field_view_field('node', $node, 'field_yourvocab', array('default'));
	$yourvocabterms = array(); // will store taxonomy terms of the current node

 	foreach ($yourvocab['#object']->field_yourvocab['und'] as $term) {
		array_push($yourvocabterms, $term['tid']);
		// adds each found term to the end of the array
	 	}
 	
   	$output = array_shift($yourvocabterms);
   	// shifts the first value off and returns it as text

    foreach ($yourvocabterms as $a) { // shifts the remaining terms off one by one
    	$output = ($output.'+'.$a); // adding a plus symbol after each previous term
	    }

 return ($output);

 }

}

This PHP creates a string like 7+74+23 depending on how many terms you've tagged the node with.

Your Block can't be tested in the Preview window, but on visiting a relevant node page with taxonomy, it should list all other nodes with any of the same tags, and exclude the current node from that list.