Author image
Senior Developer

Making a drupal views block title link back to the view

One for the small but handy category this. The standard "more" link the views module adds to it's blocks has no place on a modern accessible website, you really need a more verbose link in there. One solution has always been to add a bit of footer or header text into the block, but it's a little tedious - and clients always find views hard to edit.

This simple solution will turn the block title into a link to the view, it goes into your template.php _phptemplate_variables function

  if ($hook=='block'){
    $block = $vars['block'];
    if ($block->module == 'views'){
      $view = views_get_view($block->delta);
      if ($view->page && $view->url && $block->subject){
        $link =  l($block->subject,$view->url,array(),null,null,false,true);
        $vars['block']->subject = $link;
      }
    }
  }

[EDIT] - as per comments below, the above is Drupal 5.x only - the principle could be applied in 6.x using a preprocess hook (either in your template.php or in a simple module)

Comments

Definetly a useability improvement for any site, good thinking!

My only doubt here is.. aren't you loading the view twice with this solution? I haven't gone ahead and tried to see what is in $vars for such a block though, so I don't know if there's a better way to tackle this. Either way, something to keep in mind, thanks!

Luckily views_get_view uses a static variable to cache previously loaded views, so there is no performance hit from calling it twice ...

In 6.x, _phptemplate_callback() is no longer supported. It has been rolled in to the main theme() function. As long as the function is registered as a template, the template file will be used. _phptemplate_variables() has also been deprecated.

Someone has an example how to make this work in 6.x?

For 6.x it should probably be done in a preprocess hook.

See http://drupal.org/theme-guide for details on those. Also, the Views theme information link and the "Theme Developer" (from Devel module) should help.

Sorry - yup, should have said this is 5.x only (I will edit the origional post now) - same thing should work for 6 but you would use a preprocess function, which also means you can pop it in a module and share it between projects more easily (I love those Drupal 6 preprocess functions!)

I'd appreciate if anyone could give me an example how to do this in drupal 6.

You can do this with a preprocess function like this:

function example_preprocess_block(&$vars) {

  if (isset($vars['block']->module) && ($vars['block']->module == 'views')) {
    list($name, $display_id) = explode('-', $vars['block']->delta);
    if ($view = views_get_view($name)) {
      if ($view->access('page_1')) {
        $vars['block']->subject = l($vars['block']->subject, $view->get_url());
      }

      $view->destroy();
    }

  }
}

This will only ever link each view to the first page handler you specify for the view, a bit limited I know, but something that can be solved with a display plugin...which I write in a minute or two.

Hi,

I'm using drupal 5 and I have a zen sub theme called 'nva'.

I'm pasting

function nva_phptemplate_variables(&$vars, $hook) {
if ($hook=='block'){
    $block = $vars['block'];
    if ($block->module == 'views'){
      $view = views_get_view($block->delta);
      if ($view->page && $view->url && $block->subject){
        $link =  l($block->subject,$view->url,array(),null,null,false,true);
        $vars['block']->subject = $link;
      }
    }
  }
}

into my subthemes template.php and the block titles are not working...

I definitely have page views of the blocks.

I'm not a programmer - yet - but kind of an infant at this stuff so its probably something simple. I'd love to have this feature, so would be grateful for any pointers.

i figured it out, I hadn't read it but in my subthemes template.php it says

For a sub-theme to add its own variables, instead of _phptemplate_variables,
* use these functions:
* nva_preprocess_page(&$vars) to add variables to the page.tpl.php
* nva_preprocess_node(&$vars) to add variables to the node.tpl.php
* nva_preprocess_comment(&$vars) to add variables to the comment.tpl.php
* nva_preprocess_block(&$vars) to add variables to the block.tpl.php

so I tried the block one, it didn't work so I put the code in Main theme template.php and it works!

Thanks for the code!

Hey there, this is way awesome and thank you so much for posting these useful findings. One thing that wouldn't hurt to mention in the pages description somewhere is that the view must have both a block view (naturally) as well as a page view (also naturally) but specifically mentioning to make sure to include a page view might help some people, as this was an issue we experienced at first. I know it only makes sense, but when you see a block view with a more link you might forget that it doesn't have a page view and isn't really linking to anything.

So, this works excellent when blocks are placed within normal regions around the site. However, we are unable to get it to work while these blocks are located within a Panels page (http://drupal.org/project/panels). The Panels module displays blocks in an organized and customizable fashion, kinda like creating regions on the fly without using actual regions.

We are assuming that this is hook related or something thats somehow negated on Panels end. But none the less we are not able to figure it out and was curious if anyone might have some insight on a resolution.

Thanks so much.
Vishun

nice share brotha, I strongly believe this article highly anticipated by our blogger friends including me

Comments on this article are now closed, if you want to give us feeback you can use our contact form instead.