Experimenting with Views

Posted by gordon on Tue, 18/04/2006 - 4:27pm

Over the last weeks I have been doing a lot of experimenting with the new views module. I have actually been able to do some neat things with the headers like add an "A | B | C ..." to the heading to allow you to select on the nodes that begin with letter x, and allow a user to drill down on a list of terms.

But my latest has been I was able to turn a taxonomy into a set of tabs with only a single view. It actually works really well and is very good. Here is the code that does it.

[?php $views =& $GLOBALS['current_view'];
$args = $views->args;

$primary = taxonomy_get_tree(2, 0, -1, 1);
if ($args[0]) {
if (!($parents = taxonomy_get_parents($args[0]))) {
$parents[$args[0]] = taxonomy_get_term($args[0]);
}

$breadcrumbs = drupal_get_breadcrumb();
array_pop($breadcrumbs);
$breadcrumbs[] = l($items['items'][menu_get_active_item()]['title'], $view->url);

foreach ($parents as $term) {
$breadcrumbs[] = l($term->name, "{$view->url}/{$term->tid}");
}

drupal_set_breadcrumb($breadcrumbs);
}

if ($primary) {
echo '

    ';
    foreach ($primary as $term) {
    if ($parents[$term->tid]) {
    $primary_menu = $parents[$term->tid];
    }
    echo 'tid] ? ' class="active"' : '') .'>'. l($term->name, "{$view->url}/{$term->tid}", array('title' => $term->description)) .'';
    }
    echo '
';
}


if ($args[0] && ($children = taxonomy_get_children($primary_menu->tid))) {
echo '
    ';
    foreach ($children as $term) {
    echo 'tid == $args[0]) ? ' class="active"' : '') .'>'. l($term->name, "{$view->url}/{$term->tid}", array('title' => $term->description)) .'';
    }
    echo '
';
}
?]

It is not a real set of tabs like what the Drupal menu system creates, but it is the same <HTML> so it should always look the same as your standard tabs.

Also you need the argument handling code which will make sure that a second level term is always used.

[?php if (!$args || !$args[0]) {
$primary = taxonomy_get_tree(2, 0, -1, 1);
$term = array_shift($primary);

$args = array($term->tid);
}

return $args;
?]



Please consider submitting

Please consider submitting this as a patch to views module. This is incredibly useful stuff.

Basically by published this

Basically by published this here, which is pushed through to drupal planet. I am not sure how I can do much more than what I have already done. It is not like it is a module or source code that can be committed. It is just some code which can be cut and pasted into a view.

You might be able to skip a

You might be able to skip a lot of that poring into the global $menu object to figure out the view by checking global $current_view, which is an as-yet undocumented feature. It's actually a really recent addition.

Thanks.

I have changed to views to use the new global var to find the view.

example

a demo url would be real nice. sounds cool.

-moshe 

demo

I have done this a yet to be released client site, So I have set up a demo here.

Goto /myprojects_menu


Wow, that's pretty freakin'

Wow, that's pretty freakin' cool. Of course, I immediately spot what seems like a Views bug in the breadcrumb, so I guess I have to look into that.

Thanks, I really like the

Thanks, I really like the views module. You can just do so much. I am actually thinking of using this as my projects menu permantly

You could probably put the

You could probably put the taxonomy description in the header too and retain that compared to your actual projects menu; you don't get the description until after clicking on the tab, but if you stick it in a div and style it to stand out I think it would look nice.