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);
}
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
»
Tue, 08/25/2009 - 14:41
hi! newbie here... where 'my_search_field' come from?
»
Tue, 08/25/2009 - 19:12
my_search_field is a url variable passed in the request.
»
Post new comment