Create a Drupal 6 View Block Based on Taxonomy Term

Content

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);
  }