the new PHP block visibility settings feature seems to be very useful http://drupal.org/node/60317

i need one block to be displayed only in nodes associated with one vocabulary - which means a number of taxonomy terms.

which PHP snippet should i use or how can i modify the snippets provided in the "Associate Blocks with Forum nodes" & "Display block only for forums" pages?

thank you

Comments

sslxt’s picture

this feature is really very useful.
i use block visibility settings to restrict google ads showing on some non-content pages, for example, admin pages, user login, etc

but i'm not very experinced with PHP so I'd like to get to know PHP syntax of boolean expressions...

See my page as working example

thanks

firebus’s picture

there's probably an easier way to do this but...

if (arg(0) == 'node' && is_numeric(arg(1))) {
  $nid = arg(1);
  $node = node_load($nid);
  foreach ($node->taxonomy as $term) {
    if ($term->name == 'Term Name') {
      return TRUE;
    }
  }
}
return FALSE;

if you wanted to look for term id instead of term name, just say $term->tid.

if anyone knows of a built in function that gives you an array of term names or term ids when passed a node id or node object, please let me know!!!

you could also construct this as an SQL query, i guess.

puti’s picture

Thanks firebus, I needed a code snippet to make a block target a taxonomy view, and that worked perfectly. I used $term->tid

davegan’s picture

Here's a similar snippet using path instead of taxonomy term.

$startlist is an array to match what areas the block should appear on
$endlist is an array to match areas within $startlist that will hide the block

for instance, if you have a section called "staff" and have a contact page under "staff/contact" and "staff/about", putting "staff" in $startlist and "contact" and "about" in $endlist will show a block on all areas of "staff" except "staff/contact" and "staff/about".

 //list of  strings to match what the url starts with - block will show
$startlist = array('toolbox');
 //list of  strings to match what the url ends with - block will not show
$endlist = array ('contact','about');

if (arg(0) == 'node' && is_numeric(arg(1))) {
	$url_alias = drupal_get_path_alias('node/'.arg(1));
} elseif (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
	$url_alias = drupal_get_path_alias('taxonomy/term/'.arg(2));
}

foreach ($startlist as $start) {
	if (substr($url_alias,0,strlen($start)) == $start) {
		foreach ($endlist as $end) {
			if (substr($url_alias, strlen($url_alias) - strlen($end)) == $end) {
				return FALSE;
			}
		}
		return TRUE;
	}
}
return FALSE;

WeRockYourWeb.com’s picture

Love this script - thank you!

IE938492’s picture

The Context Module solved most of the issues without PHP. Excellent module.

http://drupal.org/project/context