Create a Drupal 6 View Block Based on Taxonomy Term

Posted in |

Your Drupal view can be expressed both as a page and as a block. You can also filter your views content according to a taxonomy term argument. And now you want to generate your block according to the presence of your taxonomy term in your URL (either within a menu path or view search block request generated URI).

Usage

Just call MyViewBlockFromURI() wherever you require it. Stick it in a block if required. Also very handy in the header of a view - you can tie the view header into the content using the taxonomy term extracted from the URI.

Code

  /*
   * embed view as a block using $term as the view argument
   */
  function MyViewBlock($term = 'all') {
    return views_embed_view('my_view_block_name', 'block', $term);
  }
  
  /*
   * get block based on the URI
   */
  function MyViewBlockFromURI() {
       
    // get the input from the URL if available
    // looking for make field
    $input = $_GET['my_search_field'];
    
    // if no input defined then get URI
    if (isset($input) == false) {
      // get uri 
      $input = $_SERVER['REQUEST_URI'];
    }

    // get terms for our vocabulary and see if
    // input contains a valid term
    $tree = taxonomy_get_tree(MY_VOCAB_ID);  // you need to know your  id
    foreach ($tree as $obj) {
      $term = strtolower($obj->name); 
      if (stripos($input, $term) !== false) {
        $make = $term;
        break;
      }
    }
    
    return MyViewBlock($make);
  }
Submitted by Jonathan Mitchell on Tue, 06/30/2009 - 11:23

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
vsotto (not verified) Says:
Wed, 09/02/2009 - 08:35

I have a vocabulary (taxonomy) name “Section” that has list of terms like “News, Sports, Opinion, Entertainment, and etch.”

I’d like to display a block with list of terms according to “Section” and display set (numbers) of nodes (ex: node titles) according to terms.

So the look would be:

News
* some news story
* some news story

Sports
* some sports story
* some sports story

Opinion
* some opinion story
* some opinion story

I have created a view that would accept arguments base on terms, argument type: Term name/synonym converted to Term ID. With your code example that i modify it a bit but, with no success.

function views_block_terms($view_name ,$term) {
return views_embed_view($view_name, 'block', $term);
}

function views_block_result($view_name, $vid) {
$terms = taxonomy_get_tree($vid); // you need to know your id
foreach ($terms as $obj) {
$term = $obj->name;
}

return views_block_terms($view_name, $term);
}

Would you please help how can I achieved the following output above?

You can also check my thread here: http://drupal.org/node/565878

Thanks,

vsotto

vsotto (not verified) Says:
Tue, 08/25/2009 - 14:41

hi! newbie here... where 'my_search_field' come from?

Jonathan Mitchell Says:
Tue, 08/25/2009 - 19:12

my_search_field is a url variable passed in the request.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
Let us know you are human.
6 + 7 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.