Display blocks based on taxonomy of current node

TDobes - December 9, 2003 - 10:49
Project:Drupal
Version:7.x-dev
Component:block.module
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Description

It would be nice if I could configure blocks to display/not to display based upon the taxonomy of the current displayed node.

Hypothetical examples of use:
* Open Source OS Web Page: I could have a block displaying a feed from a site with Linux Kernel news when viewing a node classified under the "Linux Kernel" taxonomy term (node/view/4). The block would also show when viewing all nodes classified under this taxonomy term (taxonomy/page/or/12).
* Regional News / Discussion Site: When viewing any node containing a movie review, a sidebar might display a block with links to local movie theatres. The block would also show when listing all movie reviews (the taxonomy page).

Currently, this functionality can only be accomplished by someone with the "administer blocks" permission adding the path to each node and taxonomy view page in regex form. This means that, for this to work, an administrator must update the block administration page every time a node is added for which we'd like the block displayed. This is tedious for a small site, and impossible for a large one.

How might we go about implementing a better solution?

#1

moshe weitzman - January 9, 2004 - 23:13

#2

TDobes - January 12, 2004 - 01:00

"taxo powered blocks" actually do something QUITE different from what I'd like to see. That page seems to discuss creating a block which would display links to all nodes within certain taxonomy terms. While useful, that's not what I'm attempting to accomplish. I'm looking for a further-enhanced ability to specify, as the site admin, when a block does or does not appear. (a supplement to the current regexp system)

I'd like to be able to assign a block to taxonomy term 1, then it would display:
- when viewing the taxonomy term 1 page (taxonomy/page/or/1) or a page with taxonomy term 1 items on it (taxonomy/page/or/1,2,3)
- when viewing a node associated with tid 1 (i.e. node/view/12)
- when the criteria specified by the regexp are satisfied

Hopefully, this clarifies what I was looking for.

#3

herbivorous - April 13, 2004 - 14:29

You can write custom blocks that display via

$theme->block($subject, theme_item_list($output));

but which never execute this line if certain criteria aren't met. In which case, no block appears. I discovered this essentially by accident in trying to develop a block that would appear, but some bugs gave me no result for a while. But this might be the sort of tool you're looking for.

#4

jasonmcmunn - November 14, 2004 - 21:53

I need exactly what Tom needs. I'm hoping someone has told him the info, but I can't figure out how to contact him directly. I need to know how to get the category of the current item, and based on that I can decide to display my block or not. I really need exactly what he asked origionally. Does anyone have an answer?

Thanks!

#5

TDobes - November 15, 2004 - 03:09

jasonmcmunn:
Such a feature does not currently exist in Drupal. (That's why this feature request is still active.) It can be done, but it's not particularly easy.

A hack-ish solution might be to use some custom PHP code in a block. Of course, to determine the taxonomy of a currently-displayed node, you'd have to use node_load, which would add an extra db query and thus increase page generation time. Once killes' node_load caching patch lands, this will no longer be a problem.

But... none of this makes it easier for non-PHP-savvy admins to accomplish this task.

#6

restyler - August 21, 2005 - 01:53

May be my code can help you.

<?php
$tid
= 2; //vocabulary term, where we want out block to display
if (arg(0) == 'node' && is_numeric(arg(1)))
{
 
$nid = arg(1);
 
$terms = taxonomy_node_get_terms($nid);
  if (isset(
$terms[$tid]))
  {
    echo
"We are in term #$tid, so you see this block";
  }
}
?>
 

#7

LAsan - April 1, 2008 - 08:20
Version:x.y.z» 7.x-dev

Still feature request?

#8

thatnewguy - April 15, 2008 - 05:54

In same similar situation myself

#9

thomasmurphy - December 10, 2008 - 22:00

subscribing

#10

jroth - January 12, 2009 - 17:12

subscribing

#11

docmorphe - January 30, 2009 - 00:04

subscribing

#12

emdalton - July 2, 2009 - 18:25

I've been using this code with D5:

<?php
  $myterms
= array(39, 59, 85, 114); // list the ids of the terms you want
  // This will show on all nodes having this term
 
if ((arg(0) == 'node') && is_numeric(arg(1))) {
   
$terms = taxonomy_node_get_terms(arg(1));
    foreach(
$terms as $term) {
      if (
in_array($term->tid, $myterms)) return TRUE;
    }
  }
 
// This will show on the index page for that term
 
if ((arg(0) == 'taxonomy') && (arg(1) == 'term') && (in_array(arg(2), $myterms))) {
    return
TRUE;
  }
 
// Otherwise
 
return FALSE;
?>

I've just upgraded to D6 on a staging server, and now all the blocks are showing everywhere. (I've checked my terms-- they haven't changed.) I'd really like an easier way of doing this, e.g. assigning taxonomy terms to blocks with a pick-list and configuring a setting to do this automatically for all the indicated blocks.

#13

emdalton - July 7, 2009 - 19:10

Thanks to eojthebrave, I now have working php code for my blocks in D6: http://drupal.org/node/507540

It would still be nice to be able to pick particular taxonomy terms for blocks and refer to them more easily in the block configuration, e.g. the way one can specify particular pages a block will display on (or not).

 
 

Drupal is a registered trademark of Dries Buytaert.